-2

I was wondering if it could be possible to get access to all the files in a directory from a python script.

The script is the following:

import pandas as pd
import numpy as np

#[4] main_folder=r'C:/Users/MainUser/Desktop/sediment_processing/raw_data/cohesive/'

#[6] section_name = 'UHS_TIG01_PAC07032019'
#[7] file_extension = '.xlsm'

file = main_folder + section_name + file_extension
excel = pd.read_excel(file,sheet_name='ASCII Data',header=3)

df = excel.iloc[1:,21:53]
#And more lines for calculations

What I would like to know is if it's possible to, inside this code, write something instead of lines [4], [6] and [7] that could let apply all the calculations in this code to all the excel files in my directory:

C:/Users/MainUser/Desktop/sediment_processing/raw_data/cohesive/.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 1
    _I was wondering if it could be possible to get access to all the files in a directory from a python script._ Yes, and there are plenty of resources available which could help you with that. Have you done any research? Please see [ask], [help/on-topic]. – AMC Jul 07 '20 at 22:42
  • 1
    Does this answer your question? [Using Python to execute a command on every file in a folder](https://stackoverflow.com/questions/1120707/using-python-to-execute-a-command-on-every-file-in-a-folder) – leonardofmed Jul 07 '20 at 23:09

1 Answers1

0

You can do the following:

import os

folder = '/your/folder/location/'
files = [os.path.join(folder, item) for item in os.listdir(folder)]

This will get you a list of all files in the folder and then you can do whatever you want with them.

NotAName
  • 3,821
  • 2
  • 29
  • 44