I'm creating a script that will read a bunch of .txt files from a directory. I am getting all the names of the files in the directory using
path = os.path.join(os.path.expanduser('~'), 'Desktop',)#Path to the folder here
for filename in os.listdir(path):
path = path + '/' + filenameThis is creating a new path for each file name, I am then looking to open each file within the for filename loop and extract data from it, however the filenames contain spaces so when i try to open the new path, i am getting a 'No such directory or file' error. I have also checked if the file exists using
print os.path.isdir(path)
and am being returned false for every one.
How can i solve this? thanks in advance
Note: It will take way to long to delete all the spaces from the filenames manually
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.
How do you guys deal with filenames with spaces in:
I have a file name:
/Users/me/Downloads/This is my file.xlsx
but it gets messed up as soon as it goes anywhere near a python script. The problem is, that in a company with 33,000 employees and me at the bottom of the ladder I can't expect others to just not put spaces in their filename.
I've tried using pathlib, but python seems to get in before hand so pathlib does this to the path:
from pathlib import Path
path = Path('/Users/me/Downloads/This\ is\ my\ file.xlsx')
path
/Users/me/Downloads/This\ is\ my\ file.xlsxI've also tried:
path = (r'/Users/me/Downloads/This\ is\ my\ file.xlsx')
to no avail.
How can I get the correct filename no matter where it has come from (usually windows)
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