简体   繁体   English

我如何将Python2中的file()函数改编为Python3函数?

[英]How would I adapt the file() function in Python2 to a Python3 function?

Primary Issue 主要问题

I am currently trying to run this particular Github Project on my Mac OS. 我目前正在尝试在Mac OS上运行这个特定的Github项目。 It was most certainly coded for a system running Python 2. However, I am running Python3 and I need to make a few modifications to the program. 它肯定是为运行Python 2的系统编写的。但是,我正在运行Python3,我需要对程序进行一些修改。 Most of these modifications work seamlessly, except for the one below. 除下面的修改外,大多数这些修改都可以无缝地工作。

When I run the program with this command... 当我用这个命令运行程序时......

python3 separate_dsd.py -i ./../../Ricotti\ \&\ Alburquerque\ -\ Dont\ You\ Believe\ Me.mp3 -o ./ -m ./../../model1.pkl

...I get the error NameError: name 'file' is not defined . ...我收到错误NameError: name 'file' is not defined File has been replaced with io.open within Python3. 文件已被Python3中的io.open替换。 However, simply replacing the function call with io.open is giving me an error. 但是,只需用io.open替换函数调用就会给我一个错误。

The specific line is, f=file(filename,'rb') of the code block: 具体的行是, f=file(filename,'rb')代码块的f=file(filename,'rb')

def load_model(filename):
    f=file(filename,'rb')
    params=pickle.load(f)
    f.close()
    return params

This resides in the file /DeepConvSep/examples/dsd100/separate_dsd.py . 它位于文件/DeepConvSep/examples/dsd100/separate_dsd.py Now how would I go about replacing the exact behaviour of file(filename,'rb') with a io.open ? 现在,我将如何用io.open替换file(filename,'rb')的确切行为?


Responses to Answers Below 回答下面的答案

Currently I tried to follow @abarnert's suggestions and changed my code to: 目前我试图关注@ abarnert的建议并将我的代码更改为:

def load_model(filename):
    with open(filename, 'rb') as f:
        return pickle.load(f)

But I still get the error: 但我仍然得到错误:

Traceback (most recent call last):
  File "separate_dsd.py", line 336, in <module>
    main(sys.argv[1:])
  File "separate_dsd.py", line 333, in main
    train_auto(inputfile,outdir,model,0.3,30,25,32,513)
  File "separate_dsd.py", line 250, in train_auto
    params=load_model(model)
  File "separate_dsd.py", line 19, in load_model
    params=pickle.load(f)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xbb in position 2: ordinal not in range(128)

The file constructor was already discouraged in Python 2.5 (maybe even earlier) in favor of the open function. 在Python 2.5中(甚至可能更早)已经不鼓励使用file构造函数来支持open函数。 In Python 3, it was removed entirely. 在Python 3中,它被完全删除了。 So, it should just be a matter of: 所以,它应该只是一个问题:

f=open(filename, 'rb')

Note that, unlike your second version, I did not remove the 'rb' "mode" argument. 请注意,与第二个版本不同,我没有删除'rb' “mode”参数。 That b means "binary mode", and it's even more important in Python 3 than in Python 2. Without it, you're asking it to decode the file to Unicode text (using some default encoding), which isn't going to work with a binary file—it's going to give you something like the output in the second half of your question. b的意思是“二进制模式”,并且,你问它的文件,以Unicode文本(使用一些默认编码),这是行不通的解码它更重要的在Python 3比在Python 2少了它使用二进制文件 - 它将为您提供类似于问题后半部分输出的内容。


As a side note, you may want to rewrite the function like this: 作为旁注,您可能想要重写这样的函数:

def load_model(filename):
    with open(filename, 'rb') as f:
        return pickle.load(f)

This guarantees that the file gets closed even if unpickling raises an exception. 这可以保证即使unpickling引发异常也会关闭文件。

It seems that you've got at least two options: 看来你至少有两个选择:

a) Replace the call to file() , with open() which is a built-in function in Python 3 a)用open() )替换对file()的调用,这是Python 3中的内置函数

b) Learn how to use the immensely helpful venv (virtual python environment) and create a runtime environment for this project using an instance of a Python 2 interpreter. b)学习如何使用非常有用的venv (虚拟python环境),并使用Python 2解释器的实例为该项目创建运行时环境。

If you choose the former, you must specify that you're trying to read a binary file when you call open() : 如果选择前者,则必须指定在调用open()时尝试读取二进制文件:

open(filename, 'rb')

However, given that this is probably not the only instance, in this project, of an incompatibility between your Python 3 interpreter and the source code, I would suggest creating a Python 2 environment. 但是,鉴于这可能不是本项目中唯一一个Python 3解释器与源代码不兼容的实例,我建议创建一个Python 2环境。

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

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