Move files with spaces in the name
Вопрос
I am trying to move some files in python, but they have spaces in the name. Is there any way to specifically tell python to treat a string as a file name?
listing = os.listdir(self.Parent.userTempFolderPath)
for infile in listing:
if infile.find("Thumbs.db") == -1 and infile.find("DS") == -1:
fileMover.moveFile(infile, self.Parent.userTempFolderPath, self.Parent.currentProjectObject.Watchfolder, True)
After I get the file from the listing, I run os.path.exists
on it to see if it exists, and it never exists! Can somebody give me a hint here?
Решение
The spaces in the filenames are not the problem; os.listdir
returns filenames, not full paths.
You'll need to add them to your filenames to test them; os.path.join
will do this for you with the correct directory separator for your platform:
listing = os.listdir(self.Parent.userTempFolderPath)
for infile in listing:
if 'Thumbs.db' not in infile and 'DS' not in infile:
path = os.path.join(self.Parent.userTempFolderPath, infile)
fileMover.moveFile(path, self.Parent.userTempFolderPath, self.Parent.currentProjectObject.Watchfolder, True)
Note that I also simplified your filename tests; instead of using .find(..) == -1
I use the not in
operator.
Не связан с StackOverflow