Reputation: 1418
In python subprocess using Popen or check_output, I need to list files and directories in a given source directory. But I can only use the command ls -l.
Sample code
cmd = ["ls", "-l", source]
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
stdout, stderr = proc.communicate()
exitcode = proc.returncode
if exitcode != 0:
raise SystemError("Exitcode '{}', stderr: '{}', stdout: '{}' for command: '{}'".format(
exitcode, stderr, stdout, cmd))
From above proc, by using grep or any other way, can I get only a list of files and directory names inside source directory without other information?
Upvotes: 1
Views: 5510
Reputation:
Parsing the output of ls
is a bad idea for a few reasons. If your file name has a trailing space, then ls
will display it as 'trailing space '
and if you try to open("'trailing space '")
it won't work. Also file names can contain newlines.
Use pathlib
instead:
from pathlib import Path
source = Path("/path/to/some/directory")
[x.name for x in source.iterdir()]
# ['a_file', 'some_other_file.txt', 'a_directory']
Upvotes: 2
Reputation: 1254
As Charles Duffy mentioned, you can use os. Like this.
import os
directory=#wherever you want to search
files_and_directories=os.listdir(directory)
Out: ['Directories and file names in a list']
Upvotes: 1
Reputation: 5535
If you insist on using subprocess please try:
[x.split(' ')[-1] for x in stdout.decode().split('\n')[1:-1]]
Obviously this is a pretty "hacky" way of doing this. Instead I can suggest the standard library glob
import glob
glob.glob(source + '/*')
returns a list of all file/directory names in source.
Edit:
cmd = ["ls", source]
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
stdout, stderr = proc.communicate()
exitcode = proc.returncode
stdout.decode("utf-8").split('\n')[:-1]
Should also do it. -l
option is not necessary here.
Upvotes: 3