简体   繁体   English

Python编译的字节码:如果存在非ASCII字符,则无法读取文件

[英]Python compiled bytecode: Unable to read file if non-ascii character are present

First, a bit of background: I work in japan, so folders with japanese characters are common (something like 作業フォルダ, "working folder" ). 首先,有一点背景知识:我在日本工作,所以带日语字符的文件夹很常见(例如作业フォルダ,“工作文件夹”之类)。

I have a python program that I then compile to bytecode. 我有一个python程序,然后将其编译为字节码。 If I execute this compiled file from the windows command line, it runs without trouble: 如果我从Windows命令行执行此编译文件,则该文件运行不会出现问题:

for example: 例如:

python.exe C:\\作業フォルダ\\myProgram.py python.exe C:\\作业フォルダ\\ myProgram.py

however, once compiled with the following command: 但是,使用以下命令编译后:

python.exe -OO -m py_compile myProgram.py python.exe -OO -m py_compile myProgram.py

I get the following error: 我收到以下错误:

python.exe C:\\作業フォルダ\\myCompiledProgram.pyc python.exe C:\\作业フォルダ\\ myCompiledProgram.pyc
python: Can't reopen pyc file python:无法重新打开pyc文件

if I go into the 作業フォルダ folder and execute like this: 如果我进入作业フォルダ文件夹并按以下方式执行:

python.exe myCompiledProgram.pyc python.exe myCompiledProgram.pyc

It runs without problems. 它运行没有问题。 However, I would like it to run in the case i give the Absolute path, and moreover i want to pass file paths as parameters, which also generates errors like: 但是,我希望它在我提供绝对路径的情况下运行,而且我想将文件路径作为参数传递,这也会产生如下错误:

RuntimeError: Unable to open C:\\作業フォルダ\\somefile.txt RuntimeError:无法打开C:\\作业フォルダ\\ somefile.txt

Why does this happens only after compiling? 为什么只有在编译后才发生这种情况? Is there a way to fix it? 有办法解决吗?


  • If relevant, I am using windows 8.1, but happens in windows 10 computers as well, Python version is 3.6, installed with Anaconda3 5.10 如果相关,我正在使用Windows 8.1,但也发生在Windows 10计算机中,Python版本为3.6,与Anaconda3 5.10一起安装

A related answer suggests the fix is to move the script out of directories with non-ASCII characters. 一个相关的答案表明,解决方法是将脚本移出带有非ASCII字符的目录。 Junctions/symlinks can also be used to 'fake' non-ASCII directory names, but that is considerably more complicated. 连接点/符号链接也可以用于“伪造”非ASCII目录名称,但这要复杂得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM