Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

whitespaces in the path of windows filepath

Tags:

python

file

I am working on file operations using python.

I have a filepath as :

filepath = "E:/ABC/SEM 2/testfiles/all.txt" 

when I am opening the file using python, it says me :

IOError: No such file: 

but, the file is present on the drive.
It may be because windows cannnot take "SEM 2" properly as it contains space.
How can I deal with such whitespaces in the path of window path?

like image 981
sam Avatar asked Feb 13 '13 11:02

sam


People also ask

How do I find Filepath in Windows?

To view the full path of an individual file: Click the Start button and then click Computer, click to open the location of the desired file, hold down the Shift key and right-click the file. Copy As Path: Click this option to paste the full file path into a document.

How do you add a space in a Windows path?

Use quotation marks when specifying long filenames or paths with spaces. For example, typing the copy c:\my file name d:\my new file name command at the command prompt results in the following error message: The system cannot find the file specified. The quotation marks must be used.

How do you escape space in python path?

As the accepted answers states, there's no need to escape the spaces in this case. But if this was about escaping spaces, that actually does not work. You cannot just put \ in front of any character to escape it. In order to actually print a space, you would have to use '\x20' or you could use '\t' to print a tab.

How do I run a PowerShell script with spaces in path?

If you want to run powershell.exe -File from the command line, you always have to set paths with spaces in double quotes ( " ). Single quotes ( ' ) are only recognized by PowerShell. But as powershell.exe is invoked (and hence the file parameter processed) by the command line, you have to use " .


1 Answers

path = r"C:\Users\mememe\Google Drive\Programs\Python\file.csv" 

Closing the path in r"string" also solved this problem very well.

like image 79
billmanH Avatar answered Oct 13 '22 20:10

billmanH