2

I need to open and parse all files in a folder, but I have to use a relative path (something like ../../input_files/).

I know that in JavaScript you can use the "path" library to solve this problem.

How can I do it in python?

TDMNS
  • 281
  • 1
  • 6
  • 19

3 Answers3

3

This way you can get a list of files in a path as a list

You can also filter for file types

import glob

for file in glob.iglob('../../input_files/**.**',recursive=True):
    print(file)

Here you can specify the file type : **.**

for example: **.txt

Output:

../../input_files/name.type

xio
  • 630
  • 5
  • 11
1

You can use listdir from the os library, and filter out only the files which has txt as the ending

from os import listdir
txts = [x for x in listdir() if x[-3:] == 'txt']

Then you can iterate over the lists and do your work on each file.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
mama
  • 2,046
  • 1
  • 7
  • 24
1

Don't worry about the Absolute path, below line gives you the Absolute path where your script runs.

import os

script_dir = os.path.dirname(__file__)  # <-- absolute dir to the script is in

Now you can merge your relative path to the Absolute path

rel_path = 'relative_path_to_the_txt_dir'
os.path.join(script_dir, rel_path)  # <-- absolute dir to the txt is in

If you print the above line you'll see the exact path your txt files located on.

Here is what you are looking for:-

import glob
import os

script_dir = os.path.dirname(__file__)  # <-- absolute dir to the script is in
rel_path = 'relative_path_to_the_txt_dir'
txt_dir = os.path.join(script_dir, rel_path)  # <-- absolute dir to the txt is in

for filename in glob.glob(os.path.join(txt_dir, '*.txt')):  # filter txt files only
   with open(os.path.join(os.getcwd(), filename), 'r') as file:  # open in read-only mode
      # do your stuff

Here are few links that you can understand what I did:-

  1. os.path.dirname(path)
  2. os.path.join(path, *paths)
  3. glob.glob(pathname, *, recursive=False)

References:-

  1. Open file in a relative location in Python
  2. How to open every file in a folder
Kushan Gunasekera
  • 7,268
  • 6
  • 44
  • 58