Python Forum
.py pandas matplotlib .xlsx files - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: .py pandas matplotlib .xlsx files (/thread-39666.html)



.py pandas matplotlib .xlsx files - QubeStory - Mar-23-2023

I am facing a problem with my Python script and I hope someone can help me with it.

I have a list of file names, such as f1, f2, f3, etc. These files are in the xlsx format, and they are located in different folders. "THE REAL PROBLEM IS THIS F FILES HAS REACHED MORE THEN 450 IN JUST 3 MONTHS TIME AND ITS JUST ADDING HOW TO SIMPLIFY IT THIS"

I want to divide these files into two segments, red and green, and I want to add the file names manually to these two lists.

Here is an example of how the lists would look like:

All red: [f1, f4, f5] All green: [f2, f3]

I keep adding files to these two lists based on the experiment results.

Then I have a little script that I run that finds the relative path of the directories where the files are located and looks for the files in all folders.

Here is the code:

f1 = '/exm1.xlsx'
f2 = '/exm2.xlsx'
f3 = '/sub1.xlsx'
f4 = '/sub2.xlsx'
f5 = '/eco1.xlsx'
f6 = '/eco2.xlsx'
# and so on...
I manually divide these files into two categories: "red" and "green". For example:

all_red_files = [f1, f4, f5]
all_green_files = [f2, f3]
I keep adding more files based on experiment results. For example:
red_files = [f113]
I want to simplify this process as the list of files and their names keep getting bigger (e.g., f1, f2, f3, ...).

I use the following script to find the files in different directories:

# Finds relative path
# Change up one directory
os.chdir("..")
# Find current path
d = os.getcwd()

# Gets all directories in the folder as a tuple
directories = [os.path.join(d, o) for o in os.listdir(d) if os.path.isdir(os.path.join(d, o))]

# Looks for the files in all folders
file_green = []
for directory in directories:
    for file in all_green_files:
        if os.path.exists(directory + file):
            f = directory + file
            file_green.append(f)
            print('file found:\n', f)

file_red = []
for directory in directories:
    for file in red_files:
        if os.path.exists(directory + file):
            f = directory + file
            file_red.append(f)
            print('file found:\n', f)
How can I simplify this process as my list of files and their names keep getting bigger?


RE: .py pandas matplotlib .xlsx files - buran - Mar-23-2023

Cross-posted at StackOverflow

For start I can repeat what I commented there as well
Quote:Keep adding [hardcoded and seemingly random] file names manually into categories is recipe for disaster. Use standard name pattern to programmatically separate red/green categories (or separate the files at the time of creation in different folders). Consider using a database for the results (instead of files).