• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 445
  • Last Modified:

Python searching files


Trying to call search1file(root, filename) to search for "Directory" in the files that end in .txt i found in main()

I get an error saying no such file or directory 'author.txt' ... but that is definitely i file in there


import os
import fnmatch

def main():
    path = os.environ['SANDBOX'] 
    for root, dirs, files, in os.walk(path):
        print
        print root
        for filename in files:
            if filename.endswith('.txt'):
                print "     ", filename
                search1file( root,filename)

def search1file( root,filename):
    file1 = open(filename, "r")
    readString = file1.readline()
    if 'DIRECTORY' in line:
        print "        ", line
    file1.close()
    


    
main()

Open in new window

0
Stephen1991
Asked:
Stephen1991
  • 2
1 Solution
 
peprCommented:
The os.walk() returns the lists of subdirectories and of the filenames as the lists of bare names.  You should use the os.path.join(root, filename) to get the filename with the path (possibly in the search1file(). Otherwise the open() tries to find the bare filename -- i.e. inside the current working directory, which can be the visited directory only sometimes ;)

By the way, your root should not be named root as it is the just-visited directory by the recursive os.walk().
0
 
Stephen1991Author Commented:
Hmm interesting thank you.  My python knowledge is very limited.  In the previous problem I had the path using the os.path.join  


def main():
    path = os.path.join(os.environ['SANDBOX'],'CS402')   
    for root, dirs, files, in os.walk(path):
        print
        print root
        for filename in files:
            f = filename.lower()
            if f.endswith('.txt'):
                print "     ", filename
                
                
                

Open in new window



And trying to edit it for the whole sandbox while searching for directory within the text files.  
0
 
peprCommented:
The problem is with the body of the loop.  The os.walk(path) returns a tuple with three elements (for each iteration; see http://docs.python.org/library/os.html#os.walk).  The documentation says:

For each directory in the tree rooted at directory top (including top itself), it yields a 3-tuple (dirpath, dirnames, filenames).

dirpath is a string, the path to the directory. dirnames is a list of the names of the subdirectories in dirpath (excluding '.' and '..'). filenames is a list of the names of the non-directory files in dirpath. Note that the names in the lists contain no path components. To get a full path (which begins with top) to a file or directory in dirpath, do os.path.join(dirpath, name).

Notice the emphasized note.  The os.path.join() must be applied to the results inside the for loop, like this:

import os
import fnmatch

def main():
    path = os.environ['SANDBOX'] 
    for root, dirs, files, in os.walk(path):
        print
        print root
        for filename in files:
            if filename.endswith('.txt'):
                fname = os.path.join(root, filename)
                print "     ", fname
                printFirstLineIfContainsDIRECTORY(fname)

def printFirstLineIfContainsDIRECTORY(filename):
    f = open(filename, "r")
    line = f.readline()
    if 'DIRECTORY' in line:
        print "        ", line
    f.close()
    

if __name__ == '__main__':
    main()

Open in new window


I did not run the script, it may contain some bugs. Feel free to ask.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now