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?
(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)
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
path = r"C:\Users\mememe\Google Drive\Programs\Python\file.csv"
Closing the path in r"string" also solved this problem very well.
Source: Stackoverflow.com