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>
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).
This has nothing to do with the directory name containing spaces. When you run python3 prog.py or file prog.py, you're specifying a relative path, and standard Unix semantics mean that it will be opened from the current working directory. There's no sensible way to change that.
However, if you make the script executable (chmod +x /path/to/prog.py) and ensure that it has a correct #! line at the top (probably #! /usr/bin/python3) then you will be able to execute it from anywhere by saying just prog.py (not python3 prog.py).
Incidentally, it's normally a good idea for programs on $PATH not to contain an extension that specifies their implementation, since that's really a layering violation; I'd recommend calling it just prog (replace as appropriate) rather than prog.py.
I believe the issue is not with the space. Have you tried a path without a space?
The point is, $PATH are the paths to look for executables, not files of random kind.
For example, if you have an executable that complains about a missing library, you have to use $LD_LIBRARY_PATH to tell it where the file is. Adding the location of the file to $PATH won't help. On the other hand if you come across a missing library in compile time, you need another variable (namely the $LIBRARY_PATH).
So the best solution would be to add proper shebangs to your python scripts and put them somewhere in the $PATH. If you want to be able to import them anywhere you need to work with $PYTHONPATH.
Hey all - stupid question but how do I deal with spaces when I am using file paths?
I have a script I'm working on converting from Bash and one file that I need to past in from input is in the Application Support folder in my user directory -
So basically I have thisfileDir = input("Please pass the file directory here ")
fileDirReciever = fileDir+"/system_logs.logarchive.foo.txt
When I print FileDirReceiver it prints out :
Application\ Support/
But when I pass the variable into an open command
open (fileDirReciever ,"r")
FileNotFoundError: [Errno 2] No such file or directory:.../Application\\ Support/
I've tried using r to make it a literal with no luck as well. Thanks
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?
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.
I typically get rid of space for space for most folder path.
Just want to confirm if there is an issue with space in folder path.