Python - Create Multiple Folders From Csv File
I want to create multiple folders/directories (if they don't exist) using information from a CSV file. I have the information from csv as follows: Column0 Column1 Column2 Colum
Solution 1:
The following approach should help get you started:
- Open the CSV file and skip the header row.
- Read a row, splitting it into named columns.
- If the
file_url
containsinput
, use a sub folder ofinput
, etc. - Create a folder based on
output_root
and the sub folder name. - Use a Python
Counter
to keep track of the number of times each sub folder is used. - Add the current sub folder count to the folder name and create any necessary output folders.
- Use the Python
requests
library to download the text file from the website. - Extract the filename from the URL and use this to write the file contents.
The script is as follows:
from collections import Counter
import requests
import csv
import os
output_root = r'/myroot'
output_counter = Counter()
withopen('limitedresult.csv', newline='') as csvfile:
readCSV = csv.reader(csvfile)
header = next(readCSV)
for number, test, col2, file_url in readCSV:
if'completed'in file_url:
sub_folder = 'input'elif'uploads'in file_url:
sub_folder = 'output'else:
sub_folder = Noneprint('Invalid URL -', file_url)
if sub_folder:
output_folder = os.path.join(output_root, test, sub_folder)
output_counter.update([output_folder])
output_folder += str(output_counter[output_folder])
os.makedirs(output_folder, exist_ok=True)
data = requests.get(file_url)
file_name = os.path.split(file_url)[1]
withopen(os.path.join(output_folder, file_name), 'w') as f_output:
f_output.write(data.text)
Note, you may need to install requests
, this can usually be done using pip install requests
.
Post a Comment for "Python - Create Multiple Folders From Csv File"