Skip to content

Instantly share code, notes, and snippets.

@bobhancockgist
Created April 12, 2013 19:55
Show Gist options
  • Save bobhancockgist/5374660 to your computer and use it in GitHub Desktop.
Save bobhancockgist/5374660 to your computer and use it in GitHub Desktop.
Python: files in dir
import os
import sys
def main():
if len(sys.argv) < 2:
sys.stderr.write("usage: scriptname source_dir")
sys.exit(1)
source_dir = sys.argv[1]
try:
files = os.listdir(source_dir)
except OSError as e:
sys.stderr.write("Cannot retrieve file names: "+str(e))
sys.exti(1)
for f in files:
fname = os.path.join(source_dir, f)
with open(fname, "r") as fh:
pass # this is where you do stuff
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment