how to proccess the blank in the path on linux

Z

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.
 
J

Jeremy Sanders

A.T.Hofkamp said:
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.
 
T

TheSaint

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top