472,127 Members | 1,763 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,127 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 1894
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Hal Vaughan | last post: by
31 posts views Thread by John Roth | last post: by
1 post views Thread by Martijn Ras | last post: by
6 posts views Thread by Konrad Hinsen | last post: by
reply views Thread by Berlin Brown | last post: by
reply views Thread by leo001 | last post: by

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.