- 1 year ago
How to return a list of csv files contained in a folder as a list and be able to set a variable from the list
Comment
1 / 1
To return a list of CSV files contained in a folder as a list and set a variable from that list in Python, you can use the glob
module. Here's an example:
pythonimport glob
folder_path = "path/to/folder" # Replace with the actual folder path
csv_files = glob.glob(folder_path + "/*.csv")
if csv_files:
# Set a variable to the list of CSV files
csv_files_list = csv_files
print(csv_files_list) # Display the list of CSV files
else:
print("No CSV files found in the folder.")
In the above code, folder_path
is the path to the folder where you want to search for CSV files. The glob.glob
function is used to retrieve a list of file paths matching the specified pattern (*.csv
in this case). If there are CSV files found in the folder, the csv_files_list
variable is set to the list of file paths. You can then use the csv_files_list
variable for further processing.
Make sure to replace "path/to/folder"
with the actual path to the folder containing the CSV files.