Really wonky file handing

Please, oh people of wisdom, enlighten this poor neophyte.

I’m opening a file and it opens just fine. But if the file name starts with n it does not (I think the machine is seeing it as \n. How would I get around it?
with open(‘C:\Coursea\TwitterSen\negative_words.txt’) as negitiveFile:

Exception has occurred: OSError
[Errno 22] Invalid argument: ‘C:\Coursea\TwitterSen\negative_words.txt’
File “C:\Coursea\TwitterSen\testopen.py”, line 1, in
with open(‘C:\Coursea\TwitterSen\negative_words.txt’) as positiveFile:

Indeed, \n is interpreted as a newline character, which isn’t something allowed in filenames. There’s three ways to solve this. First, double the backslashes (\\n), so the second is escaped by the first. Second, use /, which Windows accepts as a separator. Finally, you can prefix the string with r (like r"C:\Coursea\Twitter.."), which disables backslash escapes for that string.

2 Likes

you have the right idea – in python string literals, the slash is an escape, so that “\n” is a new line.

To debug this, try making the filename first and then printing it:

In [15]: filename = "C:\Coursea\TwitterSen\negative_words.txt"

In [16]: print(filename)
C:\Coursea\TwitterSen
egative_words.txt

So Windows paths are a challenge.

The “right way” to deal with file paths is with a library – check out the pathlib module, for instance.

But the quick and dirty way is to use a “raw” string:

In [13]: filename = r"C:\Coursea\TwitterSen\negative_words.txt"

In [14]: print(filename)
C:\Coursea\TwitterSen\negative_words.txt

The ‘r’ in front of the quotes means “raw string”, which means don’t interpret the slashes as escapes, just keep them as they are.

2 Likes

Easiest fix: Don’t use backslashes in paths. Windows is quite happy using forward slashes like the rest of the world does:

with open(‘C:/Coursea/TwitterSen/negative_words.txt’) as positiveFile:

And you don’t have conflicts with escape sequences that way.

1 Like

I wouldn’t call it “happy”. Forward slash is only ever supported in the Windows API by creating a normalized copy of a path. The native NT API has no support at all for using forward slash as a path separator. Also, even in the Windows file namespace, forward slash is not supported in “\\?\” extended paths and not supported by various file API functions, such as the entire path API. Also, third-party applications and libraries may not support forward slash in paths (e.g. on the command line), possibly because they rely on the path API. It’s easy to just normalize paths. pathlib.Path always does this. Otherwise use os.path.normpath().

3 Likes

Yep, a more cross-platform way would be to use the path libraries:

os.path.join("C:", "Coursea", "Twittersen", "negative_words.txt")
pathlib.Path("C:", "Coursea", "Twittersen", "negative_words.txt")
pathlib.Path("C:") / "Coursea" / "Twittersen" / "negative_words.txt"

They will use whatever directory separator is appropriate, correctly handle joining paths together, etc.

2 Likes

thank you for the

r

trick :slight_smile: