简体   繁体   中英

How to run script for all files in a folder/directry

I am new to python. I have successful written a script to search for something within a file using :

open(r"C:\\file.txt) and re.search function and all works fine.

Is there a way to do the search function with all files within a folder? Because currently, I have to manually change the file name of my script by open(r"C:\\file.txt), open(r"C:\\file1.txt) , open(r"C:\\file2.txt)`, etc.

Thanks.

You can use os.walk to check all the files, as the following:

import os
for root, _, files in os.walk(path):
    for filename in files:
        with open(os.path.join(root, filename), 'r') as f:
            #your code goes here

Explanation :

os.walk returns tuple of (root path, dir names, file names) in the folder , so you can iterate through filenames and open each file by using os.path.join(root, filename) which basically joins the root path with the file name so you can open the file.

You can use the os.listdir(path) function:

import os

path = '/Users/ricardomartinez/repos/Salary-API'

# List for all files in a given PATH
file_list = os.listdir(path)

# If you want to filter by file type
file_list = [file for file in os.listdir(path) if os.path.splitext(file)[1] == '.py']


# Both cases yo can iterate over the list and apply the operations
# that you have

for file in file_list:
    print(file)
    #Operations that you want to do over files

Since you're a beginner, I'll give you a simple solution and walk through it.

Import the os module, and use the os.listdir function to create a list of everything in the directory. Then, iterate through the files using a for loop.

Example:

# Importing the os module
import os

# Give the directory you wish to iterate through
my_dir = <your directory -  i.e. "C:\Users\bleh\Desktop\files">

# Using os.listdir to create a list of all of the files in dir
dir_list = os.listdir(my_dir)

# Use the for loop to iterate through the list you just created, and open the files
for f in dir_list:
    # Whatever you want to do to all of the files

If you need help on the concepts, refer to the following:

for looops in p3: http://www.python-course.eu/python3_for_loop.php

os function Library (this has some cool stuff in it): https://docs.python.org/2/library/os.html

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM