9

This is my second post, I apologize if I do something incorrectly - I will try to be as concise as possible.

I did some searching, and most escapes have to deal with embedded JSON strings - my problem is actually with opening the file itself.

At present, I'm trying to make my code as generic as possible, so I'm using:

file = open(os.path.expanduser(r'~/Desktop/Austin/Tweets/10_7_2012_12/09-Tweets.txt'), 'r')

The issue is that when the interpreter sees this code, it sees the "/" in the file name, and I think it's trying to go down another directory. I confirmed this by removing the "/" in the file name and typing:

file = open(os.path.expanduser(r'~/Desktop/Austin/Tweets/10_7_2012_1209-Tweets.txt'), 'r')

And it loaded just fine.

The problem with doing that for all of these files is that I have several hundred files containing several thousand tweets, and it's a little impractical.

So my question is this: Is there a way in which to load files with forward slashes in their file name?

I saw many ways to load files with the search button, but none including how to deal with a forward slash in the name... I've tried:

file = open(os.path.expanduser('~/Desktop/Austin/Tweets/10_7_2012_12\/09-Tweets.txt'), 'r')

and

file = open(os.path.expanduser('~/Desktop/Austin/Tweets/10_7_2012_12//09-Tweets.txt'), 'r')

All to no avail.

An explanation as to how Python handles forward slashes would be welcome, if anyone cares to teach a naive undergraduate.

I'm using a Mac OSX on Leopard. I'm running a web crawler that's communicating with the Twitter Streaming API; the slashes in the names are a result of saving them with a "/" to designate the date.

SOLUTION: You can use forward slashes in filenames on Mac OSX. From the filesystem's perspective, the / is actually a colon, and it gets translated to a / in Finder.

Kindall's explanation below: It's necessary for the Carbon layer, which uses the standard Mac filename separators, colons. Slashes have historically been allowed in Mac filenames going back to 1984. Mac users also expect to see colons, not slashes, as pathname separators in the GUI (or at least they did in 2001, when this behavior was instituted).

Noc
  • 519
  • 7
  • 18
  • 6
    Python doesn't "handle" forward slashes. It just passes the path to the underlying OS, which does whatever it does. The OSs I'm familiar with wouldn't have the problem, since you can't create a file with such a name to begin with. What OS are you using and how are you creating such files? The easiest solution would be not to create them in the first place. – kindall Jul 10 '12 at 17:56
  • I'm using a Mac OSX on Leopard. I'm running a web crawler that's communicating with the Twitter Streaming API; the slashes in the names are a result of saving them with a "/" to designate the date. – Noc Jul 10 '12 at 20:23
  • 2
    @KalîlÁrmstrøng you should add that to the question. You should replace the slashes in the names with another character that is allowed in filenames. – Colin Dunklau Jul 11 '12 at 01:19
  • Done, and thanks to Russel, I've figured it out. I appreciate your help. – Noc Jul 11 '12 at 16:40

2 Answers2

6

I'm assuming you're using a Unix-like OS, and my understanding is that forward slashes aren't allowed in filenames in such systems. What do you see if you ls in ~/Desktop/Austin/Tweets/?

Russell Borogove
  • 18,516
  • 4
  • 43
  • 50
  • better yet, do an `os.listdir('~/Desktop/Austin/Tweets/')` – inspectorG4dget Jul 10 '12 at 17:58
  • 2
    Forward slashes are not allowed in *NIX file systems and are not allowed in Windows file systems either. – steveha Jul 10 '12 at 18:02
  • It gives me all the files listed in that directory, if that's of any use. – Noc Jul 10 '12 at 20:25
  • 1
    With forward slashes in the filenames? – Russell Borogove Jul 11 '12 at 01:38
  • Russel, that was the key - once you said with forward slashes in the file name, I understood what you were getting at. It turns out that I *can* use forward slashes in file names, but for some reason when I ls ~/Desktop/Austin/Tweets/ the forward slashes in the file names are replaced with ":". The code then works as intended with ":" instead of "/" in the file name. I appreciate your help! I suppose that's more of a Mac related question, as someone mentioned earlier. – Noc Jul 11 '12 at 16:42
  • 2
    Oh, god, translating colons to slashes in Finder is a horrible misfeature. :( – Russell Borogove Jul 11 '12 at 17:14
  • 1
    It's necessary for the Carbon layer, which uses the standard Mac filename separators, colons. Slashes have historically been allowed in Mac filenames going back to 1984. Mac users also expect to see colons, not slashes, as pathname separators in the GUI (or at least they did in 2001, when this behavior was instituted). – kindall Jul 12 '12 at 22:54
  • But in OSX, when you see a pathname (which isn't all that often, actually), you see slashes, so Classic users' expectations are already dashed. It's aggravating to have two different characters at the filesystem level show as the same character in some parts of the UI but not in others. – Russell Borogove Jul 13 '12 at 01:10
2

Personally I'd rather run a batch rename tool on those files. Slash in a file name is generally a bad idea.

Otherwise, the answer is r'~/Desktop/Austin/Tweets/10_7_2012_12:09-Tweets.txt'. cf. Special characters in OSX filename ? (Python os.rename)

Community
  • 1
  • 1
Qnan
  • 3,714
  • 18
  • 15
  • A raw string prefixed with `r` won't need escaping for the backslash. A normal string would though. – Mark Ransom Jul 11 '12 at 16:01
  • @MarkRansom good point. Although it seems that Python interprets the slash as a directory delimiter anyways. – Qnan Jul 11 '12 at 16:08
  • @MikhailKozhevnikov: Python should not interpret the `/`, it should pass that to the OS. Any reference for the `\/` escape trick? – Fred Foo Jul 11 '12 at 16:10
  • Mikhail, your edited answer is correct - I was able to deduce that from Russel's trouble shooting above, as well. – Noc Jul 11 '12 at 16:43