提问人:BrinyFlyer28795 提问时间:5/27/2023 最后编辑:BrinyFlyer28795 更新时间:5/27/2023 访问量:62
“OSError: [Errno 22] Invalid argument” - 重复文件?
"OSError: [Errno 22] Invalid argument" - duplicate files?
问:
尝试为我的文件创建备份:
name = input("Enter the name:" (\"/\" to quit)\n>>>")
name += ".vcblr"
write_file(name, ([], [], [], []))
def write_file(file, content) -> None:
file = open(f"PythonVocabulary/{file}", "wb")
pickle.dump(content, file)
file = open(f"PythonVocabulary/backup/{file}", "wb")
pickle.dump(content, file)
我收到错误:
OSError: [Errno 22] Invalid argument: "PythonVocabulary/backup/<_io.BufferedWriter name='PythonVocabulary/german.vcblr'>"
复制文件有问题吗?
答:
1赞
azro
5/27/2023
#1
你对 .因此,请使用不同的变量名称,并使用 so 文件描述符会自动关闭file
open
with
def write_file(filename, content) -> None:
with open(f"PythonVocabulary/{filename}", "wb") as file:
pickle.dump(content, file)
with open(f"PythonVocabulary/backup/{filename}", "wb") as file:
pickle.dump(content, file)
评论
<_io.BufferedWriter name='PythonVocabulary/german.vcblr'>
file
variable
file
open
filename
<>
with
with open(fname) as f: pickle.dump(...)
with open(...) as file_backup