How to open each file in a folder?

I have a python script parse.py which in a script open a file, say file1, and then do something, maybe print the total number of characters.

filename = 'file1' f = open(filename, 'r') content = f.read() print filename, len(content) 

Right now, I'm using stdout to direct the result to my output file - output

 python parse.py >> output 

However, I don’t want to do this file manually manually, is there a way to take care of each individual file automatically? how

 ls | awk '{print}' | python parse.py >> output 

Then the problem is, how could I read the file name from the standard? or are there already some built-in functions to facilitate ls and those types of work?

Thank!

+113
python file pipe stdin stdout
Aug 15
source share
6 answers

You can list all the 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 template, using the glob :

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

It does not have to be the current directory, which you can list in any way:

 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 channel as you indicated using fileinput

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

And then use it with piping:

 ls -1 | python parse.py 
+272
Aug 15 '13 at 21:38
source share

you should try using os.walk

 yourpath = 'path' import os for root, dirs, files in os.walk(yourpath, topdown=False): for name in files: print(os.path.join(root, name)) stuff for name in dirs: print(os.path.join(root, name)) stuff 
+28
Aug 16 '13 at 13:15
source share

In fact, you can simply use the os module to do both:

  1. list all files in a folder
  2. sort files by file type, file name, etc.

Here is 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 



Now you have not only listed all the files in the folder, but also sorted them (optionally) by name, file type and others. Just go through each list and do your job.

+7
Apr 17 '17 at 20:27
source share

I searched for this answer:

 import os,glob folder_path = '/some/path/to/file' for filename in glob.glob(os.path.join(folder_path, '*.htm')): with open(filename, 'r') as f: text = f.read() print (filename) print (len(text)) 

You can also select "* .txt" or other ends of your file name.

+6
May 31 '18 at 13:57
source share

A simple solution

If you want to just open all the files in the root of the directory. I came across this problem many times, so I created an easy-to-use module for Python 3.5 and Python 2.7. If your version of Python is not supported, just ask me on GreyCadet IRC and I will add support.

Module installation

 pip install filemapper 

Using

Consider a directory structure like this, and this main.py is your code.

 -Program -resources nouns.txt config.dat help.txt main.py 

Main.py content

 import filemapper as fm all_files = fm.load('resources') # fm.load('resources','w') will open in write mode for f in all_files: for i in fm.read(f):print i 

This will print the lines of each file in the resource folder. You can also transfer any mode.

Doing more

If you want to do more than just open files with this module, refer to the file file on the GitHub page for more details.

+1
Jun 16 '16 at 17:41
source share
 import pyautogui import keyboard import time import os import pyperclip os.chdir("target directory") # get the current directory cwd=os.getcwd() files=[] for i in os.walk(cwd): for j in i[2]: files.append(os.path.abspath(j)) os.startfile("C:\Program Files (x86)\Adobe\Acrobat 11.0\Acrobat\Acrobat.exe") time.sleep(1) for i in files: print(i) pyperclip.copy(i) keyboard.press('ctrl') keyboard.press_and_release('o') keyboard.release('ctrl') time.sleep(1) keyboard.press('ctrl') keyboard.press_and_release('v') keyboard.release('ctrl') time.sleep(1) keyboard.press_and_release('enter') keyboard.press('ctrl') keyboard.press_and_release('p') keyboard.release('ctrl') keyboard.press_and_release('enter') time.sleep(3) keyboard.press('ctrl') keyboard.press_and_release('w') keyboard.release('ctrl') pyperclip.copy('') 
+1
Jun 08
source share



All Articles