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
  • 317
  • 1
  • 6
  • 15

3 Answers3

2

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

HSNHK
  • 602
  • 4
  • 10
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
  • 704,970
  • 168
  • 1,303
  • 1,425
mama
  • 1,608
  • 1
  • 4
  • 17
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
  • 3,907
  • 2
  • 31
  • 40