473,288 Members | 1,718 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,288 software developers and data experts.

how to proccess the blank in the path on linux

zhf
I want ro walk a directory and its sub directory on linux,
to find some shell script file, and run them, but I found some path belong
blank charactor, such as '8000 dir', if I write as follow, I got error
"no such file"
path = '8000 dir'
for root, dirs, files in os.walk(path):
cmd = ' '
cmd = 'cd ' + root
os.system(cmd)

How can I repair it?

thanks, best regards.

Jun 27 '08 #1
3 1967
On 2008-05-21, zhf <my*******@sina.comwrote:
I want ro walk a directory and its sub directory on linux,
to find some shell script file, and run them, but I found some path belong
blank charactor, such as '8000 dir', if I write as follow, I got error
"no such file"
path = '8000 dir'
for root, dirs, files in os.walk(path):
cmd = ' '
cmd = 'cd ' + root
os.system(cmd)

How can I repair it?
Escape the space to prevent the shell from interpreting it as a word seperator.
This of course also holds for all other shell meta characters,
such as * [ ] \ < & and a few others I probably have forgotten.
I am not sure why you execute 'cd' commands in a sub-shell, as it is not very
useful. (that is, you start a new sub-process, in that sub-process you run the
'cd' command (causing the cwd of the sub-process to change directory), and then
the sub-process ends)

You most likely want to change the cwd of the Python process. If so, have a
look at os.chdir(). That function has the added advantage that there is no
shell in between that interprets all those meta characters (that is,
"os.chdir('8000 dir')" will work without further effort).

Sincerely,
Albert
Jun 27 '08 #2
A.T.Hofkamp wrote:
Escape the space to prevent the shell from interpreting it as a word
seperator. This of course also holds for all other shell meta characters,
such as * [ ] \ < & and a few others I probably have forgotten.
If the command was useful (unlike cd), it might be better to use subprocess
to launch it so that you don't need the escaping:

subprocess.call(['ls', '8000 dir'])

This avoids using the shell.

--
Jeremy Sanders
http://www.jeremysanders.net/
Jun 27 '08 #3
On 22:26, mercoledì 21 maggio 2008 zhf wrote:
I want ro walk a directory and its sub directory on linux
os.path.walk() should do the job.
Recursively you should try this, which I found on some web site:

8<---------8<---------8<---------8<---------8<---------8<---------

def file_find(folder, fname):
"""search for a filename fname starting in folder"""
for root, dirs, files in os.walk(folder):
for file in files:
# make search case insensitive
if fname.lower() == file.lower():
return Path.join(root, fname)

8<---------8<---------8<---------8<---------8<---------8<--------

Definitely I don't know about path with spaces or Unicode file names.
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Jean-Marc Molina | last post by:
Hello, I can't find a way to execute a Windows application, whose directory path contains blank spaces, from a PHP script. I also wonder if the problem happens under Linux and other OS. ...
4
by: Hal Vaughan | last post by:
I want to have a config file for my program, which means I need to know where the config file is. If I type: java myclass and it runs myclass.class, is there any way to obtain the location of...
31
by: John Roth | last post by:
I'm adding a thread for comments on Gerrit Holl's pre-pep, which can be found here: http://tinyurl.com/2578q Frankly, I like the idea. It's about time that all of the file and directory stuff...
1
by: Martijn Ras | last post by:
Heya folks, I ran into the following problem: When i run this on Windows everything is as expected: C:\>python Python 2.2.3 (#42, May 30 2003, 18:12:08) on win32 Type "help", "copyright",...
5
by: Stan | last post by:
hi, Could any one give me an advice of providing a regular expression that will return all non-blank lines in a file in linux. Maybe it is just a command! Thanks Stan
34
by: Ben Sizer | last post by:
I've installed several different versions of Python across several different versions of MS Windows, and not a single time was the Python directory or the Scripts subdirectory added to the PATH...
6
by: Konrad Hinsen | last post by:
I am trying to install Python from sources in my home directory on a Mac cluster (running MacOS X 10.4.8). The path to my home directory contains a blank, and since the installation procedure...
0
by: Berlin Brown | last post by:
I am trying to run some basic unit tests, but I can't get the paths setup in python/cygwin to pick up my modules. This code works fine in linux and I installed python through cygwin not as part...
1
by: thread | last post by:
Hi i built a form that getting dynamic SQL string,the SQL string is placed in the record source when the form is opened. i notice when trying to reduce some proccess time from the form that it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.