Search code examples
pythonfileread-write

Python doesn't create file if not existing


I cannot figure out how to create file that does not exist. I tried following, yet I get error that file does not exist.

Please guide.

f=open('c:\Lets_Create_Malware\output.txt', 'r+')

f=open('c:\Lets_Create_Malware\output.txt', 'w+')

f=open('c:\Lets_Create_Malware\output.txt', 'a+')

f=open('c:\Lets_Create_Malware\output.txt', 'r')

f=open('c:\Lets_Create_Malware\output.txt', 'w')

f=open('c:\Lets_Create_Malware\output.txt', 'a')

Solution

  • Use a double backslash:

    f=open('c:\\Lets_Create_Malware\\output.txt', 'w+')
    

    From the docs:

    The backslash (\) character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character.