0

I have a folder, which i want to select manually, with an X number of .txt files. I want to make a program that allows me to run it -> select my folder with files -> And cycle through all files in the folder and take a value from a set place.

I have already made a piece of code that allows me to take the value from the .txt file:

mylines = []                             
with open ('test1.txt', 'rt') as myfile: 
    for myline in myfile:                
        mylines.append(myline)           
subline = mylines[58]
sub = subline.split(' ')
print(sub[5])`

EDIT: I also have a piece of code that makes a list of directories with all the files I want to use this on:

''' import glob

path = r'C:/Users/Etienne/.spyder-py3/test/*.UIT' files = glob.glob(path)

print(files) '''

How can I use the first piece of code on every file in the list from the second piece of code so i end up with a list of values?

I never worked with coding but this would make my work a lot faster so I want to pick up python.

Etienne
  • 1
  • 1

1 Answers1

0

If I understood the problem correctly, the os module might be helpful for you. ***os.listdir() method in python is used to get the list of all files and directories in the specified directory.For example;

import os

# Get the list of all files and directories
# in the root directory, you can change your directory
path = "/"
dir_list = os.listdir(path)

print("Files and directories in '", path, "' :") 

#   print the list
print(dir_list)

with this list you can iterate your txt files.

To additional information you can click

How can I iterate over files in a given directory?