whitespaces in the path of windows filepath
Question:
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?
Answers:
Try putting double quotes in your filepath variable
""E:/ABC/SEM 2/testfiles/all.txt""
Check the permissions of the file or in any case consider renaming the folder to remove the space
There is no problem with whitespaces in the path since you’re not using the “shell” to open the file. Here is a session from the windows console to prove the point. You’re doing something else wrong
Python 2.7.2 (default, Jun 12 2011, 14:24:46) [MSC v.1500 64 bit (AMD64)] on wi
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>>
>>> os.makedirs("C:/ABC/SEM 2/testfiles")
>>> open("C:/ABC/SEM 2/testfiles/all.txt","w")
<open file 'C:/ABC/SEM 2/testfiles/all.txt', mode 'w' at 0x0000000001D95420>
>>> exit()
C:UsersGnibbler>dir "C:ABCSEM 2testfiles"
Volume in drive C has no label.
Volume Serial Number is 46A0-BB64
Directory of c:ABCSEM 2testfiles
13/02/2013 10:20 PM <DIR> .
13/02/2013 10:20 PM <DIR> ..
13/02/2013 10:20 PM 0 all.txt
1 File(s) 0 bytes
2 Dir(s) 78,929,309,696 bytes free
C:UsersGnibbler>
path = r"C:UsersmememeGoogle DriveProgramsPythonfile.csv"
Closing the path in r”string” also solved this problem very well.
(WINDOWS – AWS solution)
Solved for windows by putting tripple quotes around files and paths.
Benefits:
1) Prevents excludes that quietly were getting ignored.
2) Files/folders with spaces in them, will no longer kick errors.
aws_command = 'aws s3 sync """D:/""" """s3://mybucket/my folder/" --exclude """*RECYCLE.BIN/*""" --exclude """*.cab""" --exclude """System Volume Information/*""" '
r = subprocess.run(f"powershell.exe {aws_command}", shell=True, capture_output=True, text=True)
a hack on Mac:
path = '/Volumes/Public/ABY/Documents/Musiq/slumdog millonaire/Mausam and Escape.mp3'
nPath = path.replace(' ', '\ ')
print(nPath)
Output:
/Volumes/Public/ABY/Documents/Musiq/slumdog millonaire/Mausam and Escape.mp3
double back slash \
will solve the problem
subprocess.run('type "C:\Users\nameofuser\blackdrive - somecorp\Desktop\seadlines_data.txt"',shell=True)
I found a simple hack try adding a single quote before the the double quotes like :
os.system(r'"C:Program FilesGoogleChromeApplicationchrome.exe"')
worked for me
you can try """
filepath = """E:/ABC/SEM 2/testfiles/all.txt"""
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?
Try putting double quotes in your filepath variable
""E:/ABC/SEM 2/testfiles/all.txt""
Check the permissions of the file or in any case consider renaming the folder to remove the space
There is no problem with whitespaces in the path since you’re not using the “shell” to open the file. Here is a session from the windows console to prove the point. You’re doing something else wrong
Python 2.7.2 (default, Jun 12 2011, 14:24:46) [MSC v.1500 64 bit (AMD64)] on wi
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>>
>>> os.makedirs("C:/ABC/SEM 2/testfiles")
>>> open("C:/ABC/SEM 2/testfiles/all.txt","w")
<open file 'C:/ABC/SEM 2/testfiles/all.txt', mode 'w' at 0x0000000001D95420>
>>> exit()
C:UsersGnibbler>dir "C:ABCSEM 2testfiles"
Volume in drive C has no label.
Volume Serial Number is 46A0-BB64
Directory of c:ABCSEM 2testfiles
13/02/2013 10:20 PM <DIR> .
13/02/2013 10:20 PM <DIR> ..
13/02/2013 10:20 PM 0 all.txt
1 File(s) 0 bytes
2 Dir(s) 78,929,309,696 bytes free
C:UsersGnibbler>
path = r"C:UsersmememeGoogle DriveProgramsPythonfile.csv"
Closing the path in r”string” also solved this problem very well.
(WINDOWS – AWS solution)
Solved for windows by putting tripple quotes around files and paths.
Benefits:
1) Prevents excludes that quietly were getting ignored.
2) Files/folders with spaces in them, will no longer kick errors.
aws_command = 'aws s3 sync """D:/""" """s3://mybucket/my folder/" --exclude """*RECYCLE.BIN/*""" --exclude """*.cab""" --exclude """System Volume Information/*""" '
r = subprocess.run(f"powershell.exe {aws_command}", shell=True, capture_output=True, text=True)
a hack on Mac:
path = '/Volumes/Public/ABY/Documents/Musiq/slumdog millonaire/Mausam and Escape.mp3'
nPath = path.replace(' ', '\ ')
print(nPath)
Output:
/Volumes/Public/ABY/Documents/Musiq/slumdog millonaire/Mausam and Escape.mp3
double back slash \
will solve the problem
subprocess.run('type "C:\Users\nameofuser\blackdrive - somecorp\Desktop\seadlines_data.txt"',shell=True)
I found a simple hack try adding a single quote before the the double quotes like :
os.system(r'"C:Program FilesGoogleChromeApplicationchrome.exe"')
worked for me
you can try """
filepath = """E:/ABC/SEM 2/testfiles/all.txt"""