2

I have a one folder, within it contains 5 sub-folders. Each sub folder contains some 'x.txt','y.txt' and 'z.txt' files and it repeats in every sub-folders Now I need to read and print only 'y.txt' file from all sub-folders. My problem is I'm unable to read and print 'y.txt' files. Can you tell me how solve this problem.

Below is my code which I have written for reading y.txt file

import os, sys
import pandas as pd

file_path = ('/Users/Naga/Desktop/Python/Data')
for root, dirs, files in os.walk(file_path):    
    for name in files:       
       print(os.path.join(root, name))
       pd.read_csv('TextInformation.txt',delimiter=";", names = ['Name', 'Value'])

error :File TextInformation.txt does not exist: 'TextInformation.txt'

Rakesh
  • 81,458
  • 17
  • 76
  • 113
v naga
  • 47
  • 5
  • you forgot to use your new path as a path for your file in `pd.read_csv('TextInformation.txt',delimiter=";", names = ['Name', 'Value'])`, it should be: `pd.read_csv(os.path.join(root, name),delimiter=";", names = ['Name', 'Value'])` – Oleg Vorobiov Jul 10 '19 at 08:11

3 Answers3

0

You could also try the following approach to fetch all y.txt files from your subdirectories:

import glob
import pandas as pd

# get all y.txt files from all subdirectories
all_files = glob.glob('/Users/Naga/Desktop/Python/Data/*/y.txt')

for file in all_files:
    data_from_this_file = pd.read_csv(file, sep=" ", names = ['Name', 'Value'])
    # do something with the data

Subsequently, you can apply your code to all the files within the list all_files. The great thing with glob is that you can use wilcards (*). Using them you don't need the names of the subdirectories (you can even use it within the filename, e.g. *y.txt). Also see the documentation on glob.

trotta
  • 1,232
  • 1
  • 16
  • 23
  • It depends whether you want to stick to your approach of using `pandas`. If you simply want to read the txt-file line by line you could also consider an approach like [this](https://stackoverflow.com/questions/3277503/how-to-read-a-file-line-by-line-into-a-list). – trotta Jul 10 '19 at 08:30
  • I added a for loop to the code to iterate over the paths. You could simply use `read_csv` from pandas to read data from the files. However, as I don't know how your files are structured, you would have to adapt the code to your needs. – trotta Jul 10 '19 at 08:36
0

Your issue is forgot adding the parent path of 'y.txt' file. I suggest this code for you, hope it help.

import os
pth = '/Users/Naga/Desktop/Python/Data'
list_sub = os.listdir(pth)
filename = 'TextInformation.txt'
for sub in list_sub:
    TextInfo = open('{}/{}/{}'.format(pth, sub, filename), 'r').read()
    print(TextInfo)
Lê Tư Thành
  • 1,063
  • 2
  • 10
  • 19
0

I got you a little code. you can personalize it anyway you like but the code works for you.

import os
for dirPath,foldersInDir,fileName in os.walk(path_to_main_folder):
if fileName is not []:
    for file in fileName:
        if file.endswith('y.txt'):
            loc = os.sep.join([dirPath,file])
            y_txt = open(loc)
            y = y_txt.read()
            print(y)

But keep in mind that {path_to_main} is the path that has the subfolders.

Amir Esmaeili
  • 88
  • 2
  • 8