Herrvorragend
Herrvorragend

Reputation: 1

Is there a way to be able to use a variable path using os

The goal is to run through a half stable and half variable path.

I am trying to run through a path (go to lowest folder which is called Archive) and fill a list with files that have a certain ending. This works quite well for a stable path such as this.

fileInPath='\\server123456789\provider\COUNTRY\CATEGORY\Archive

My code runs through the path (recursive) and lists all files that have a certain ending. This works well. For simplicity I will just print the file name in the following code.

import csv
import os

fileInPath='\\\\server123456789\\provider\\COUNTRY\\CATEGORY\\Archive
fileOutPath=some path

csvSeparator=';'

fileList = []

for subdir, dirs, files in os.walk(fileInPath):
 for file in files:

    if file[-3:].upper()=='PAR':
        print (file)

The problem is that I can manage to have country and category to be variable e.g. by using *

Upvotes: 0

Views: 47

Answers (1)

Tim
Tim

Reputation: 2637

The standard library module pathlib provides a simple way to do this.

Your file list can be obtained with

from pathlib import Path

list(Path("//server123456789/provider/".glob("*/*/Archive/*.PAR"))

Note I'm using / instead of \\ pathlib handles the conversion for you on windows.

Upvotes: 1

Related Questions