Braiden Bessey
Braiden Bessey

Reputation: 21

opening files using python / IDLE returns "no such file or directory" for pathname

I have WORDS_TXT = /macintosh HD/users/[username]/Desktop/[file]/words.txt/

but when run, python says "no such file or directory", however going though finder and "go to folder" that exact pathname brings me to the file I am trying to open. I am running python 3.2 on a macbook pro with Mac OS X 10.7

thank you in advance

Upvotes: 2

Views: 10158

Answers (4)

Prasanna
Prasanna

Reputation: 1

Try this:

WORDS_TXT = '\macintosh HD\users/[username]\Desktop/[file]\words.txt\'

Upvotes: 0

Mark Ransom
Mark Ransom

Reputation: 308528

If the filename was read from another file, there's a chance that you have an end of line character or whitespace at the end of the string. You should remove this plus the extraneous '/' at the end of the filename.

WORDS_TXT = WORDS_TXT.rstrip(' \r\n/')

Upvotes: 1

Bryan Oakley
Bryan Oakley

Reputation: 386372

Rest assured, python has no limitation in this regard. About the only two possibilities are:

  1. There is a hard-to-see typo in your code so you are looking for the wrong file, or
  2. The code is correct and the file really doesn't exist because there is a hard-to-see typo in the actual file name. For example, maybe the file on disk has a leading or trailing space in the name.

Upvotes: 0

sampwing
sampwing

Reputation: 1268

WORDS_TXT = '/macintosh HD/users/[username]/Desktop/[file]/words.txt/'
WORDS_TXT.rstrip('/')

Upvotes: 1

Related Questions