Python - Create multiple folders from CSV file
<p>I want to create multiple folders/directories (if they don't exist) using information from a CSV file.</p>

<p>I have the information from csv as follows:</p>

<pre><code>    Column0 Column1 Column2 Column3
    51  TestName1   0   https://siteAdress//completed/file.txt
    53  TestName2   0   https://siteAdress//completed/file.txt
    67  TestName1   2   https://siteAdress//uploads/file.txt
    68  TestName1   2   https://siteAdress//uploads/file.txt
</code></pre>

<p>I want to iterate column3, if it contains 'uploads' then it should make a folder with the corresponding jobname mentioned on column1 then create 'input' folder and within it create respective file.txt file, and if column3 contains 'completed' then it should make 'output' folder (within that same jobname folder next to input folder) and then within it the 'file.txt' file. And do this for all jobs mentioned in column1.</p>

<p>Something like this:</p>

<pre><code>TestName1/input/file.txt
TestName1/output/file.txt
TestName1/output2/file.txt

TestName2/input/file.txt
TestName2/output/file.txt
</code></pre>

<p>Note: Most of data will contain multiple output folders for each jobname. In this case it should create as many output folders as are mentioned in the csv file.</p>

<p>So far, I have done this:</p>

<pre><code>import csv, os
#reads from csv file
with open('limitedresult.csv') as csvfile:
    readCSV = csv.reader(csvfile, delimiter = ',')
    for row in readCSV:
        print(row)
</code></pre>

<p>Your help would be highly appreciated, please let me know if the question is still confusing and I will try to explain in more detail.</p>

<p>The following approach should help get you started:</p>

<ol>
<li>Open the CSV file and skip the header row.</li>
<li>Read a row, splitting it into named columns.</li>
<li>If the <code>file_url</code> contains <code>input</code>, use a sub folder of <code>input</code>, etc.</li>
<li>Create a folder based on <code>output_root</code> and the sub folder name.</li>
<li>Use a Python <code>Counter</code> to keep track of the number of times each sub folder is used.</li>
<li>Add the current sub folder count to the folder name and create any necessary output folders.</li>
<li>Use the Python <code>requests</code> library to download the text file from the website.</li>
<li>Extract the filename from the URL and use this to write the file contents.</li>
</ol>

<p>The script is as follows:</p>

<pre><code>from collections import Counter
import requests
import csv
import os

output_root = r'/myroot'
output_counter = Counter()

with open('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 = None
            print('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]

            with open(os.path.join(output_folder, file_name), 'w') as f_output:
                f_output.write(data.text)
</code></pre>

<p>Note, you may need to install <code>requests</code>, this can usually be done using <code>pip install requests</code>.</p>

python csv