Python Forum
dealing with spaces in file names
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
dealing with spaces in file names
#1
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
Reply
#2
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')
Reply
#3
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
Reply
#4
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
(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 
Reply
#6
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  rename same file names in different directories elnk 0 145 Nov-04-2022, 05:23 PM
Last Post: elnk
  UnicodeEncodeError - Dealing with Japanese Characters fioranosnake 2 378 Jul-07-2022, 08:43 PM
Last Post: fioranosnake
  Dealing with duplicated data in a CSV file bts001 10 4,810 Sep-06-2021, 12:11 AM
Last Post: SamHobbs
  Saving Excel workbook file with dataframe names Biplab1985 0 1,468 Jun-07-2020, 12:25 PM
Last Post: Biplab1985
  Dealing with a .json nightmare... ideas? t4keheart 10 2,845 Jan-28-2020, 10:12 PM
Last Post: t4keheart
  Details of attachment files in a msg file such as file names save into a python list klllmmm 2 4,222 Nov-12-2019, 05:59 AM
Last Post: klllmmm
  splitstring file names a by hyphen steve22020 3 2,412 Oct-30-2019, 05:39 PM
Last Post: steve22020
  Is there a more effecient way to do this ? File Names sumncguy 2 1,475 Jul-11-2019, 12:47 PM
Last Post: DeaD_EyE
  How to combine file names into a list from multiple directories? python_newbie09 3 4,011 Jul-09-2019, 07:38 PM
Last Post: python_newbie09
  Dealing with Exponential data parthi1705 11 6,895 May-30-2019, 10:16 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020