Ich versuche, den Unix-Befehl find
in Python und nicht zur Eingabe pwd
, `pwd`
funktioniert auch nicht.Verwenden von Unix finden `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"')
Ihre commands.getoutput Linie für mich funktioniert. hast du es in der Python-Shell versucht? – trans1st0r
Wie andere vorgeschlagen haben, gibt es in Python fast sicher Methoden, um das zu tun, was Sie versuchen - z. 'os.listdir',' os.walk', das 'glob'-Modul, etc. Aber wenn Sie diesem Ansatz verpflichtet wären, könnten Sie auch etwas wie' commands.getoutput ('find "% s" -name "* .txt "'% os.getcwd())'. 'print' die Saite, die ich dir vorschlage, an 'getoutput' zu übergeben, um zu sehen, was es tut. – jedwards
Sie sind Ritus, es funktioniert in Python-Shell.Ich verwende dieses Programm in Tcsh. Ich weiß nicht, ob das etwas damit zu tun hat. – Deep