tkbx
tkbx

Reputation: 16285

Loading file to string, no such file?

I'm working on a python script to thoroughly mutilate images, and to do so, I'm replacing every "g" in the file's text with an "h" (for now, it will likely change). Here's the beginning, and it's not working:

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

x = open(pathToFile, 'r')
print x

After giving the path (dragging file into the terminal), this is the result:

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 '

How can the file not exist if it's right there, and I'm using the exact filename?

Upvotes: 0

Views: 177

Answers (1)

senderle
senderle

Reputation: 151077

Look closely: '/Users/me/Desktop/file.jpeg '. There's a space in your filename. open doesn't do any stripping.

>>> 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 '

Upvotes: 6

Related Questions