Python - Need to loop through directories looking for TXT files

I am a total Python Newb

I need to loop through a directory looking for .txt files, and then read and process them individually. I would like to set this up so that whatever directory the script is in is treated as the root of this action. For example if the script is in /bsepath/workDir, then it would loop over all of the files in workDir and its children.

What I have so far is:

#!/usr/bin/env python import os scrptPth = os.path.realpath(__file__) for file in os.listdir(scrptPth) with open(file) as f: head,sub,auth = [f.readline().strip() for i in range(3)] data=f.read() #data.encode('utf-8') pth = os.getcwd() print head,sub,auth,data,pth

This code is giving me an invalid syntax error and I suspect that is because os.listdir does not like file paths in standard string format. Also I dont think that I am doing the looped action right. How do I reference a specific file in the looped action? Is it packaged as a variable?

Any help is appriciated


import os, fnmatch def findFiles (path, filter): for root, dirs, files in os.walk(path): for file in fnmatch.filter(files, filter): yield os.path.join(root, file)

Use it like this, and it will find all text files somewhere within the given path (recursively):

for textFile in findFiles(r'C:\Users\poke\Documents', '*.txt'): print(textFile)


os.listdir expects a directory as input. So, to get the directory in which the script resides use:

scrptPth = os.path.dirname(os.path.realpath(__file__)) <hr>

Also, os.listdir returns just the filenames, not the full path. So open(file) will not work unless the current working directory happens to be the directory where the script resides. To fix this, use os.path.join:

import os scrptPth = os.path.dirname(os.path.realpath(__file__)) for file in os.listdir(scrptPth): with open(os.path.join(scrptPth, file)) as f: <hr>

Finally, if you want to recurse through subdirectories, use os.walk:

import os scrptPth = os.path.dirname(os.path.realpath(__file__)) for root, dirs, files in os.walk(scrptPth): for filename in files: filename = os.path.join(root, filename) with open(filename, 'r') as f: head,sub,auth = [f.readline().strip() for i in range(3)] data=f.read() #data.encode('utf-8')


  • Why does Python's os.walk fail silently?
  • Skip a specified number of columns with numpy.genfromtxt() python 3.4 error
  • I want to import obj files in blender
  • Convert different encodings to ascii
  • Perl seg fault while joining threads
  • Using Office Word to read doc files with PHP
  • How to delete the symlink along with the source directory
  • How to read data from socket connection - android
  • C# where to add a method
  • python cdll can't find module
  • How to authenticate user name and password against Active Directory Federation Services (ADFS)?
  • Django: DRY principle and UserPassesTestMixin
  • vi mode to emacs mode while on R
  • Django return user model id with L
  • Tell Git to stop prompting me for conflicts when none really exist?
  • Laravel: Getting Session ID oddly truncates when using foreach
  • Reading a file into a multidimensional array
  • Jquery UI tool tip close icon
  • Display issues when we change from one jquery mobile page to another in firefox
  • How to set/get protobuf's extension field in Go?
  • How to redirect a user to a different server and include HTTP basic authentication credentials?
  • script to move all files from one location to another location
  • Sending data from AppleScript to FileMaker records
  • ILMerge & Keep Assembly Name
  • Running a C# exe file
  • Can I make an Android app that runs a web view in Chrome 39?
  • Symfony2: How to get request parameter
  • Google cloud sdk not working when python points python3
  • Why winpcap requires both .lib and .dll to run?
  • Apache 2.4 - remove | delete | uninstall
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • Cannot Parse HTML Data Using Android / JSOUP
  • Load html files in TinyMce
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • How do you join a server to an Active Directory (domain)?
  • Understanding cpu registers
  • coudnt use logback because of log4j
  • LevelDB C iterator
  • Linking SubReports Without LinkChild/LinkMaster
  • How to Embed XSL into XML