Deep Deep - 7 months ago 24
Python Question

Using Unix find `pwd`

I am trying to use the Unix

find
command in Python and can't get to input
pwd
,
`pwd`
did not work either.

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"')

M.T M.T
Answer

Just to put it out as an alternative.

Assuming you only want to search in the one directory (ie. not recursive) I would rather try using glob (assuming line ends with /, otherwise that would need to be added to the string):

import glob
for line in files:
    print(glob.glob(line+"*.txt"))

if recursive (Python3.5>):

import glob
for line in files:
    print(glob.glob(line+"**/*.txt"),recursive=True)