path = r"C:\Users\mememe\Google Drive\Programs\Python\file.csv"
Closing the path in r"string" also solved this problem very well.
Answer from billmanH on Stack Overflowpath = r"C:\Users\mememe\Google Drive\Programs\Python\file.csv"
Closing the path in r"string" also solved this problem very well.
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:\Users\Gnibbler>dir "C:\ABC\SEM 2\testfiles"
Volume in drive C has no label.
Volume Serial Number is 46A0-BB64
Directory of c:\ABC\SEM 2\testfiles
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:\Users\Gnibbler>
Windows path with spaces in python - Stack Overflow
How can i handle filepath while has spaces followed with hyphen
Why does some Python software not allow white spaces in file paths?
How can I work around white space in a filepath?
Why does my Python script fail to open a file with spaces in the path?
Is it necessary to use raw strings for file paths in Python?
Can I use the pathlib module for file operations in Python?
Particularly with Anaconda and Python, having a space anywhere in the file path causes the entire program to just implode, causes some of the launchers to not work, and is generally a giant pain in the ass.
This is especially weird because most systems allow white space in folder and file names with no issue (i.e. Windows and its "Program Files" install directory, for example).
I wrote a program that creates a directory/sub-directories in a specified location. I wrote it so that it allows for users to input what they want the directory to be named and then opens the newly created folder in file explorer, however, if a user chooses a file name that includes spaces I get an error.
import os
file = input(“Please enter file name: “)
user_id = os.getlogin()
filepath = f”C:/Users/{user_id}/Desktop”
path = os.path.join(filepath,file)
try:
os.makedirs(path)
except FileExistsError:
print(“This file name already exists”)
os.system(f”start C:/Users/{user_id}/Desktop/{file}”)I tried using the replace method to insert escape characters and looked into pathlib but couldn’t figure out how to get around the spaces.
The error I get is a Windows error, not Python, but I’ve included it anyways (the file name I tried was TEST FOLDER).
Windows cannot find ‘C:/Users/12345/Desktop/TEST.’ Make sure you typed the name correctly, and then try again.
There's no backslash escaping in the Windows console, it should read
"C:\Documents and Settings\Administrator\Desktop\bracket\python\python" "C:\Documents and Settings\Administrator\Desktop\bracket\[10,20]\brackettest.py"
Looks like you almost had it; just replace the double-slashes with single slashes.
If you're ever unsure and need a reminder of the right format. Try using the tab key to automatically complete your folder or filename. For example, at your command prompt just type:
C:\docu
then hit the tab key and it will automatically complete your folder name. From there you just type in the rest. Whenever you want to automatically complete the rest of the folder or filename, just hit the tab key. This will always produce a properly specified path that is runnable.
I'm trying to write some code to open a file with its default program, but it can't be opened because the filename has spaces in it.
import subprocess subprocess.Popen(['start', 'C:\\Folder\\This File'], shell=True)
Running this just opens the command prompt for some reason. It opens the file just fine if I remove the spaces, but I want to leave the spaces in the filename. I've googling solutions but none are for this function I'm trying to use. How do I go about this?