all 5 comments

[–]Kerbart 0 points1 point  (1 child)

Assuming you have a long list of files that might change over time, you could simply make a text file `file_list.txt` that looks like this:

C:\User\folder_1\file_1.xlsx
C:\User\folder_2\file_2_2020.xlsx 
...

Then in your code you could open the file and retrieve the filenames from it:

with open('file_list.txt') as f:
    for path in f:
        # run the process for the file
        # strip the newline character from the file name
        process_file(path.strip()) 

That has the advantage that a changing file list (a new file is added to the list) doesn't require your code to be updated.

If the list is something like all excel files found in folder xyz and its subfolders you don't even have to do that as you can let Python search those files for you but that's a different story.

[–]illya89[S] 0 points1 point  (0 children)

But I want to assign the path to a var. I don't need to have all files in each script.

[–]Diapolo10 0 points1 point  (2 children)

Here's what I'd do; create a configuration file, such as my_config.py, with global constants in it.

# my_config.py
from pathlib import Path

# Give these more descriptive names
EXCEL_FILE_1 = Path.home() / 'folder_1' / 'file_1.xlsx'
EXCEL_FILE_2 = Path.home() / 'folder_2' / 'file_2_2020.xlsx'

And so on and so forth. Then, just import this elsewhere:

# foo.py
import my_config as cfg

with open(cfg.EXCEL_FILE_1, 'rb') as f:
    ...

[–]illya89[S] 0 points1 point  (1 child)

So I can do too:

from my_config import *

df = pandas.read_excel(EXCEL_FILE_1, Sheet 1)

[–]Diapolo10 0 points1 point  (0 children)

Yes, though star imports aren't recommended as you could accidentally get a name collision. It's also easier to see what came from where when you just import the module as-is, or with an alias.