Get only the txt file you want from the folder containing the txt file – Python

I have a folder with a .txt files. the name of the files are:

my_file1.txt
my_file2.txt
my_file3.txt
my_file4.txt

In this way, only the last number is different.

import pickle

my_list = []
with open("/Users/users_a/Desktop/website-basic/sub_domain/sub_domain01.txt", "rb") as f1, 
open("/Users/users_a/Desktop/website-ba\
sic/sub_domain/sub_domain02.txt", "rb") as f2, open("/Users/users_a/Desktop/website- 
basic/sub_domain/sub_domain03.txt", "rb") as f3:
    my_list.append(pickle.load(f1))
    my_list.append(pickle.load(f2))
    my_list.append(pickle.load(f3))

print(my_list)

In this way, I load a file and put it in the my_list variable to make a list and work. As the number of files to work increases, the code becomes too long and cumbersome.

Is there an easier and more pythonic way to load only the desired txt file??

>Solution :

You can use os.listdir():

import os
import pickle

my_list = []
path = "/Users/users_a/Desktop/website-basic/sub_domain"

for file in os.listdir(path):
    if file.endswith(".txt"):
        with open(f"{path}/{file}","r") as f:
            my_list.append(pickle.load(f))

Where file is the filename of a file in path

I suggest using os.path.join() instead of hard coding the file paths


If your folder only contains the files you want to load you can just use:

for file in os.listdir(path):
    with open(f"{path}/{file}","r") as f:
        my_list.append(pickle.load(f))

Edit for my_file[number].txt

If you only want files in the form of my_file[number].txt, use:

import os
import re
import pickle

my_list = []
path = "/Users/users_a/Desktop/website-basic/sub_domain"

for file in os.listdir(path):
    if re.match(r"my_file\d+.txt", file):
        with open(f"{path}/{file}","r") as f:
            my_list.append(pickle.load(f))

Online regex demo https://regex101.com/r/XJb2DF/1

Leave a Reply