Answers for "how to include all files in a directory in python"

2

python import all files in directory

# Basic syntax:
import glob # Package for Unix-style pathname pattern expansion
import os   # Python operating system interface

directory = '/path/to/directory/with/files/'
# Obtain list of filenames that end in .txt in the directory
all_files = glob.glob(os.path.join(directory, "*.txt"))
# Where os.path.join creates the os-specific path to each file

# Import data however you like. To append data in a single pandas 
# dataframe and a single list, you can do:
your_list = [ ]
for filename in all_files:
    dataframe = pd.read_csv(filename, index_col=None, header=0, sep="t")
    your_list.append(dataframe)
Posted by: Guest on May-12-2021

Code answers related to "how to include all files in a directory in python"

Python Answers by Framework

Browse Popular Code Answers by Language