相关疑难解决方法(0)

NameError:未定义全局名称'unicode' - 在Python 3中

我正在尝试使用名为bidi的Python包.在这个包中的模块(algorithm.py)中,有一些行给出了错误,尽管它是包的一部分.

以下是这些行:

# utf-8 ? we need unicode
if isinstance(unicode_or_str, unicode):
    text = unicode_or_str
    decoded = False
else:
    text = unicode_or_str.decode(encoding)
    decoded = True
Run Code Online (Sandbox Code Playgroud)

这是错误信息:

Traceback (most recent call last):
  File "<pyshell#25>", line 1, in <module>
    bidi_text = get_display(reshaped_text)
  File "C:\Python33\lib\site-packages\python_bidi-0.3.4-py3.3.egg\bidi\algorithm.py",   line 602, in get_display
    if isinstance(unicode_or_str, unicode):
NameError: global name 'unicode' is not defined
Run Code Online (Sandbox Code Playgroud)

我应该如何重写这部分代码,以便它在Python3中工作?如果有人使用Python 3的bidi包,请告诉我他们是否发现了类似的问题.我感谢您的帮助.

python unicode bidi nameerror python-3.x

120
推荐指数
3
解决办法
16万
查看次数

Python open()给出IOError:Errno 2没有这样的文件或目录

出于某种原因,我的代码无法打开一个简单的文件:

这是代码:

file1 = open('recentlyUpdated.yaml')
Run Code Online (Sandbox Code Playgroud)

错误是:

IOError: [Errno 2] No such file or directory: 'recentlyUpdated.yaml'
Run Code Online (Sandbox Code Playgroud)
  • 当然,我检查了这是文件的正确名称.
  • 我试过移动文件,给出文件open()的完整路径,似乎没有任何工作.

python file-io file filenotfoundexception file-not-found

56
推荐指数
3
解决办法
30万
查看次数

Windows上的Python中的长路径

在使用Windows运行的Python编程时遇到问题.我需要使用超过256或whatsathelimit字符的文件路径.现在,我基本上阅读了两个解决方案:

  1. 使用kernel32.dll中的GetShortPathName并以这种方式访问​​该文件.

这很好,但我不能使用它,因为我需要以某种方式使用路径

shutil.rmtree(short_path)
Run Code Online (Sandbox Code Playgroud)

其中short_path是一个非常短的路径(类似的东西D:\tools\Eclipse),长路径出现在目录本身(该死的Eclipse插件).

  1. 前面"\\\\?\\"的路径

我没有设法以任何方式完成这项工作.以这种方式做任何事情的尝试总是会导致错误WindowsError: [Error 123] The filename, directory name, or volume label syntax is incorrect: <path here>

所以我的问题是:如何让第二个选项工作?我强调我需要以与选项#1中的示例相同的方式使用它.

要么

还有其他方法吗?

编辑:我需要解决方案在Python 2.7中工作

编辑2:在Windows打破Python长文件名支持的问题确实给出了"魔术前缀"的答案,我说我在这个问题中知道它.我不知道的是我如何使用它.我已经尝试将其添加到路径中,但它只是失败了,正如我上面所写的那样.

python windows

14
推荐指数
2
解决办法
1万
查看次数

matplotlib savefig - 没有这样的文件或目录

我的 matplotlib 函数有一个非常奇怪的行为savefig。我正在做如下:

import os

# creating the output folder 
if not os.path.exists(output_folder):
   os.makedirs(output_folder)

# adding the name of the figure to the created output folder
plt.savefig(output_folder + 'forecasts_scatter_%s' % model_name)
# plt.savefig(os.path.join(output_folder, 'forecasts_scatter_%s.png' % model_name))
plt.close()
Run Code Online (Sandbox Code Playgroud)

错误:

Traceback (most recent call last):
  File "C:/Users/96171/Desktop/ministry_of_public_health/CodeUbrCorrected/bmwmlmcw.py", line 56, in <module>
    lm.cross_validation(model, hyperparameters[model_name], model_name)
  File "C:\Users\96171\Desktop\ministry_of_public_health\CodeUbrCorrected\cross_validation_smogn.py", line 554, in cross_validation
    self.cross_validation_grid(model_used, hyperparams, model_name)
  File "C:\Users\96171\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\utils\testing.py", line 348, in wrapper
    return fn(*args, **kwargs)
  File "C:\Users\96171\Desktop\ministry_of_public_health\CodeUbrCorrected\cross_validation_smogn.py", line 1444, in cross_validation_grid
    'predicted')
  File …
Run Code Online (Sandbox Code Playgroud)

python matplotlib

6
推荐指数
1
解决办法
8367
查看次数

Python FileNotFoundError如何处理长文件名

我有一个奇怪的问题。我既不能重命名特定文件,也不能删除它们。我收到FileNotFoundError。

之前已经问过类似的问题。解决此问题的方法是使用完整路径,而不仅仅是文件名。

我的脚本在仅使用文件名之前起作用,但是使用其他文件时,即使使用完整路径,也会出现此错误。

似乎文件名导致了错误,但我无法解决。

import os

cwd = os.getcwd()

file = "003de5664668f009cbaa7944fe188ee1_recursion1.c_2016-04-21-21-06-11_9bacb48fecd32b8cb99238721e7e27a3."
change = "student_1_recursion1.c_2016-04-21-21-06-11_9bacb48fecd32b8cb99238721e7e27a3."

oldname = os.path.join(cwd,file)
newname = os.path.join(cwd,change)

print(file in os.listdir())
print(os.path.isfile(file))
os.rename(oldname, newname)
Run Code Online (Sandbox Code Playgroud)

我得到以下输出:

True
False
Traceback (most recent call last):
  File "C:\Users\X\Desktop\code\sub\test.py", line 13, in <module>
    os.rename(oldname, newname)
FileNotFoundError: [WinError 2] Das System kann die angegebene Datei nicht finden: 'C:\\Users\\X\\Desktop\\code\\sub\\003de5664668f009cbaa7944fe188ee1_recursion1.c_2016-04-21-21-06-11_9bacb48fecd32b8cb99238721e7e27a3.' -> 'C:\\Users\\X\\Desktop\\code\\sub\\student_1_recursion1.c_2016-04-21-21-06-11_9bacb48fecd32b8cb99238721e7e27a3.'
[Finished in 0.4s with exit code 1]
Run Code Online (Sandbox Code Playgroud)

如果我在文件夹中使用Windows搜索,则此文件存在。如果我尝试使用完整路径,我还会收到Windows错误,找不到文件。

我还尝试将unicode字符串u''+ filename附加到字符串中,因为它是由用户建议的。

路径长度<260,那么是什么引起了问题呢?

python file-not-found

5
推荐指数
1
解决办法
539
查看次数

从长路径提取文件时,Windows上的Python Zipfile extractall IOError

我正在运行python zipfile extractall,将其解压缩到长度超过255个字符的路径。在Windows 7 64bit上运行。我要跟踪以下错误[Errno 2] No such file or directory: u'

有任何想法吗 ?

我要从中提取的网络文件夹。因此,我将该文件夹安装为网络驱动器t:\,这暂时解决了该问题。

python zipfile windows-7

2
推荐指数
1
解决办法
1114
查看次数