Given the exact paths you've specificed, at least some of your examples ought to have worked (unless the c:\Lets_Create_Malware
path doesn't exist, which would add to the confusion by causing all of your test cases to fail).
Backslashes aren't a problem here given your examples because the characters being modified aren't special:
f=open('c:\Lets_Create_Malware\output.txt', 'w')
works because \L and \o don't have special meanings and so are used literally (and the 'w' and 'a' flags will create the file if it's not already present).
However, another path:
f=open('c:\Lets_Create_Malware\badname.txt', 'w')
will fail:
IOError: [Errno 22] invalid mode ('w') or filename: 'c:\\Lets_Create_Malware\x08adname.txt'
because the \b
part of that filename gets translated as the bell character (ctrl-b or \x08).
There are two ways to avoid this problem: either precede the string with the r
raw string modifier (e.g., r'foo\bar'
) or ensure each backslash is escaped (\\
). It's preferable to use os.path.join()
from the os.path
module for this purpose.