Ad

Python Open() Gives Me Error While Opening Files Having A Space In Naming(i.e. Example Text.txt)

- 1 answer

when I use open() in python it says FileNotFound, only when I try to open files with spacing in naming, (for e.g. example text.txt)

I tried using with open('Users/vandit/Desktop/example\ text.txt', 'rb') as f:

Ad

Answer

The '\' is only used in the shell to escape the following space. In python, you can just include the whole thing in quotes: with open('Users/vandit/Desktop/example text.txt', 'w') as f:

Also (if you are on windows) if you still get an error, try prepending the string with the drive in which your Windows is installed, which is mostly C:\. So it will become: with open('C:/Users/vandit/Desktop/example text.txt', 'w') as f:

Ad
source: stackoverflow.com
Ad