Data Wrangling

How to open every file in a fold

2018-10-25  本文已影响15人  IntoTheVoid

You can list all files in the current directory using:

import os
for filename in os.listdir(os.getcwd()):
   # do your stuff

Or you can list only some files, depending on the file pattern using the glob module:

import glob
for filename in glob.glob('*.txt'):
   # do your stuff

It doesn't have to be the current directory you can list them in any path you want:

path = '/some/path/to/file'

for filename in os.listdir(path):
    # do your stuff

for filename in glob.glob(os.path.join(path, '*.txt')):
    # do your stuff

Or you can even use the pipe as you specified using fileinput

import fileinput
for line in fileinput.input():
    # do your stuff

And then use it with piping:

ls -1 | python parse.py

a simple example:

import os #os module imported here
location = os.getcwd() # get present working directory location here
counter = 0 #keep a count of all files found
csvfiles = [] #list to store all csv files found at location
filebeginwithhello = [] # list to keep all files that begin with 'hello'
otherfiles = [] #list to keep any other file that do not match the criteria

for file in os.listdir(location):
    try:
        if file.endswith(".csv"):
            print "csv file found:\t", file
            csvfiles.append(str(file))
            counter = counter+1

        elif file.startswith("hello") and file.endswith(".csv"): #because some files may start with hello and also be a csv file
            print("csv file found:\t", file)
            csvfiles.append(str(file))
            counter = counter+1

        elif file.startswith("hello"):
            print("hello files found: \t", file)
            filebeginwithhello.append(file)
            counter = counter+1

        else:
            otherfiles.append(file)
            counter = counter+1
    except Exception as e:
        raise e
        print("No files found here!")

print("Total files found:\t", counter)
上一篇下一篇

猜你喜欢

热点阅读