Python Forum

Full Version: dealing with spaces in file names
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
example

import os
file = "C:\\test file.txt"
os.system("start '" +file +"'")
Error:
The system cannot find the file 'C:\test.
im just looking to open any file with windows default applications
Should not use os.system() anymore.
Quote:os.system() and os.popen*() have been deprecated since Python 2.6 in favor of the subprocess module
There can be some problem with space in filename when using subprocess module.
So this open in default applications and can handle space in file name.
import subprocess

subprocess.run(('cmd', '/C', 'start', '', r'E:\env\test file.txt'))
A trick that also can work cross platform.
import webbrowser

webbrowser.open(r'E:\env\test file.txt')
Linux Mint test ok.
import webbrowser
 
webbrowser.open(r'/home/mint/test file.txt')
Windows only.
import os

os.startfile(r'E:\env\test file.txt', 'open')
I have tried the subprocess module as well with the same result.
The issue is the r"string space". I never actually define the string manually it's just pulled from os.listdir() so I can't add the r to the start of the string
As I know Windows paths are binary. So instead of r'' prefix, try b''. I do not know if that is changed. I didn't use Windows for years.
(Jun-01-2018, 12:57 PM)AceScottie Wrote: [ -> ]I never actually define the string manually it's just pulled from os.listdir() so I can't add the r to the start of the string
os.listdir() just return filename,so why should you add r(raw string)?
If you build path yourself then can r or \\ or / just not singel \ because of escape charterer.
Maybe i misunderstand you,so it's better that you post code and explain what's not working.

Here using listdir and open in Notepad default Windows application.
Now i just working from current folder so no path is needed.
>>> import webbrowser
>>> import os
>>> 
>>> lst = os.listdir('.')
>>> lst
['c.py', 'cpt.py', 'f.py', 'html_env', 'path.py', 'test file.txt']
>>> webbrowser.open(lst[-1])
True 
(Jun-01-2018, 01:49 PM)snippsat Wrote: [ -> ]
(Jun-01-2018, 12:57 PM)AceScottie Wrote: [ -> ]I never actually define the string manually it's just pulled from os.listdir() so I can't add the r to the start of the string
os.listdir() just return filename,so why should you add r(raw string)?
If you build path yourself then can r or \\ or / just not singel \ because of escape charterer.
Maybe i misunderstand you,so it's better that you post code and explain what's not working.

Here using listdir and open in Notepad default Windows application.
Now i just working from current folder so no path is needed.
>>> import webbrowser
>>> import os
>>> 
>>> lst = os.listdir('.')
>>> lst
['c.py', 'cpt.py', 'f.py', 'html_env', 'path.py', 'test file.txt']
>>> webbrowser.open(lst[-1])
True 

Webbroweser is the best module i was looking for.
it fixes the file name issue and also opens the "open with" window for non associated files
Thansk