0

I have the output of list1 in 1st column and I want list2 in 2nd column. How can I do this? Or does anyone has a better solution? list1 is for all folders (about 200+) in directory which are automatically generated with os.mkdir(directory). Now I want in 2nd column to check each folder for a file with .raw.txt and print Yes/No in the 2nd column. here you can see my output in csv

´   
    for list1 in fieldnames:
        for path, dir_folder, files in os.walk(path):
            for files in dir_folder:
                w.writerow(files.split())

    for list2 in fieldnames:
        for files in os.listdir(path):
            try:
                if os.path.exists("*\\raw.txt"):
                    print("Yes")
                else:
                    print("No")
            except:
                continue`
3
  • list 1 is name of folder or name of files? Commented Sep 14, 2022 at 9:39
  • list1 is name of folder in 1st column and list2 is 2nd column with Y/N Commented Sep 14, 2022 at 9:59
  • Got confused after looking at the other answer, and thought maybe list1 is file names, I have added my answer Commented Sep 14, 2022 at 11:22

3 Answers 3

1

This should be able to solve the problem.

You can always create a list of dictionary, store values in it and then write it to csv.

Here is a code that does that

import os
import csv
result_list = []
for path, dir_folder, files in os.walk('./test_folder'):
    raw_exists = "No"
    for file_name in files:
        if file_name.endswith('raw.txt'):
            raw_exists="Yes"
            break  
    row_dict={
        "Folder Name": os.path.basename(path),
        "Raw exists": raw_exists
    }
    result_list.append(row_dict)

with open('test.csv', 'w', encoding='utf8', newline='') as output_file:
    fc = csv.DictWriter(output_file, fieldnames=result_list[0].keys())
    fc.writeheader()
    fc.writerows(result_list)

Sign up to request clarification or add additional context in comments.

4 Comments

one last question, how can I remove the path in the folder name and just leave the name of the folder, not the path?
row_dict={ "Folder Name": os.path.basename(path), "Raw exists": raw_exists } Updated the answer as well
one last thing. In CSV there is the main folder(path) listed in the 1st place. is there any way to stop listing the main folder in csv??
@Volcano that I am not really sure about. Will have to dig in a bit. Also updated the code a bit to make the if condition simpler since we already get a list of files from the os.walk so we dont need extract them separately
1

Does this do what you're looking for?:

for path, dir_folder, files in os.walk(path):
    for file in files:
        w.writerow([dir_folder, "Yes" if file[-8:] == ".raw.txt" else "No"])


2 Comments

thanks, that helped a little. It adds the file xxxxx.raw.txt in column 1 (below the folder name) and shows "Yes" in column 2. How can I change it so that it doesn't add the .raw.txt file in Column1 and just shows Y/N next to the folder names?
I've updated my answer but basically just replace files with dir_folder
0

To create a CSV file showing which folders contain a file ending raw.txt you could do the following:

import csv
import os

path = '/'

with open('output.csv', 'w', newline='') as f_output:
    csv_output = csv.writer(f_output)
    
    for dirpath, dirnames, filenames in os.walk(path):
        for filename in filenames:
            if filename.endswith("raw.txt"):
                csv_output.writerow([dirpath, "Yes"])
                break
        else:
            csv_output.writerow([dirpath, "No"])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.