我正在尝试在Python中使用Unix find
命令,并且无法输入pwd
,`pwd`
也不起作用。
import commands
import os
f = raw_input('Enter name of the file: ')
fh = open(f, 'r')
prevdir = os.getcwd()
files = fh.readlines()
for line in files:
os.chdir(line)
print commands.getoutput('find `pwd` -name "*.txt"')
# print commands.getoutput('find \`pwd\` -name "*.txt"')
最佳答案
只是将其替代。
假设您只想在一个目录中搜索(即,不是递归的),我宁愿尝试使用glob
(假设行以/
结尾,否则需要将其添加到字符串中):
import glob
for line in files:
print(glob.glob(line+"*.txt"))
如果是递归的(Python3.5>):
import glob
for line in files:
print(glob.glob(line+"**/*.txt"),recursive=True)
关于python - 使用Unix查找`pwd`,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36804405/