将文件加载到字符串,没有这样的文件?

tkb*_*kbx 0 python

我正在制作一个python脚本来彻底破坏图像,为此,我用文件的文本中的每个"g"替换为"h"(现在,它可能会改变).这是开始,它不起作用:

pathToFile = raw_input('File to corrupt (drag the file here): ')

x = open(pathToFile, 'r')
print x
Run Code Online (Sandbox Code Playgroud)

给出路径(将文件拖入终端)后,结果如下:

File to corrupt (drag the file here): /Users/me/Desktop/file.jpeg 
Traceback (most recent call last):
  File "/Users/me/Desktop/corrupt.py", line 7, in <module>
    x = open(pathToFile, 'r')
IOError: [Errno 2] No such file or directory: '/Users/me/Desktop/file.jpeg '
Run Code Online (Sandbox Code Playgroud)

如果它就在那里,文件怎么可能不存在,而我正在使用确切的文件名?

sen*_*rle 6

仔细看看:'/Users/me/Desktop/file.jpeg '.你的文件名中有一个空格.open不做任何剥离.

>>> f = open('foo.txt', 'w')
>>> f.write('a')
>>> f.close()
>>> f = open('foo.txt ', 'r')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IOError: [Errno 2] No such file or directory: 'foo.txt '
Run Code Online (Sandbox Code Playgroud)

  • pathToFile = raw_input('要损坏的文件(在此拖动文件):').totrip()可以避免这种情况 (4认同)