[英]Using Unix find `pwd`
我正在嘗試在 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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.