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.
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().