简体   繁体   English

打开文本文件时“OSError:[Errno 36]文件名太长”

[英]“OSError: [Errno 36] File name too long” while opening a text file

I am trying to replace a number that is already in a text file with a new number. 我正在尝试使用新数字替换已存在于文本文件中的数字。 I opened the file and the name of the file I am opening is pretty short: "example_Na.chem", but I am still receiving the error that: OSError: [Errno 36] File name too long". 我打开文件,我打开的文件名很短:“example_Na.chem”,但我仍然收到错误:OSError:[Errno 36]文件名太长“。

The code I am using is: 我使用的代码是:

filename_chem = open('./input/example_Na/example_Na.chem', 'r').read()
input_file_chem = filename_chem.format(albeitSSA=albeitSSA)
    with open(filename_chem, 'w') as fid:
        fid.write(input_file_chem)



You are reading the content of the file ./input/example_Na/example_Na.chem and then using the file content as the file name for write-back, which can be too big for a file name. 您正在读取./input/example_Na/example_Na.chem文件的内容,然后使用文件内容作为回写的文件名,这对于文件名来说可能太大了。 If you mean to write back the formatted content back to the same file you should open the same file name for writing instead: 如果您要将格式化内容写回同一文件,则应该打开相同的文件名进行编写:

filename_chem = open('./input/example_Na/example_Na.chem', 'r').read()
input_file_chem = filename_chem.format(albeitSSA=albeitSSA)
with open('./input/example_Na/example_Na.chem', 'w') as fid:
    fid.write(input_file_chem)

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

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