Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Read all csv files, and store the last line from each folder
I want to read all csv files and read each file and capture last line form each file.
I use the below code, but I struck.

import os
from glob import glob
PATH = "D:\data\project\*\*\InputFiles"
EXT = "*.csv"
all_csv_files = [file
                 for path, subdir, files in os.walk(PATH)
                 for file in glob(os.path.join(path, EXT))]
with pathlib written here without testing, but I think it's right or close to it:
from pathlib import Path

p = Path("D:\data\project\*\*\InputFiles")
csv_files = [csvfile for csvfile in p.iterdir() if csvfile.is_file() and csvfile.suffix == '.csv']
for filename in csv_files:
I use below code. Here, the there will be 4 levels (subfolders) so I use

Exammple: Rootfolder/project/cataegory after this I do not know folder names, but there will be 4 more subfolders and in the last folder my file exist.

My file name is: .Dailycollection.log

If file exist after this : Rootfolder/project/cataegory, then I want to capture full path of the file.
p = Path("D:\Mekala_Backupdata\*\*\*\*\")
csv_files = [csvfile for csvfile in p.iterdir() if csvfile.is_file() and csvfile.suffix == '*.Dailycollection.log']
for filename in csv_files:
I can't run the following code as I don't use windows, but this should list all csv files in all directories
within and below a root directory of D:\data\project\*\*\InputFiles (you will have to supply actual values for '*')
from pathlib import Path

def walk_dir(starting_dir):
    flist = []
    for path in Path(starting_dir).iterdir():
        if path.is_file():
            if path.suffix == '.csv':
        elif path.is_dir():

    for file in flist:

if __name__ == '__main__':
    start_path = 'D:\data\project\*\*\InputFiles'
    import os
    srcdir = Path('.')
    savefile = srcdir / 'allcsvfiles.txt'
    walk_dir(start_path, savefile)
Actually the last layer (sub)directory does not know (not fixed). But the file will be saved into this last folder. Hence I want to use the wild card.
The file is available at :D:\Mekala_Backupdata\*\*\*\*\ --> here the file.

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Read KML files, edit items, and rewrite files? Winfried 4 184 Aug-21-2020, 03:55 PM
Last Post: Winfried
  Python Cut/Copy paste file from folder to another folder rdDrp 4 251 Aug-19-2020, 12:40 PM
Last Post: rdDrp
  Files to store configuration and steps for a industrial process control application Danieru 1 242 Aug-03-2020, 05:43 PM
Last Post: buran
  Read/Sort Large text file avoiding line-by-line read using mmep or hdf5 Robotguy 0 255 Jul-22-2020, 08:11 PM
Last Post: Robotguy
  Python: Automated Script to Read Multiple Files in Respective Matrices Robotguy 7 525 Jul-03-2020, 01:34 AM
Last Post: bowlofred
  How to access files from shared folder? samandhare 4 315 Jun-25-2020, 11:14 AM
Last Post: samandhare
  rename files in a folder deep_logic 2 261 Jun-01-2020, 08:44 PM
Last Post: deep_logic
  Read Multiples Text Files get specific lines based criteria zinho 5 469 May-19-2020, 12:30 PM
Last Post: zinho
  Read a CSV file only from the 4th line/row asaforen7 8 700 Apr-16-2020, 01:49 AM
Last Post: Larz60+
  Iterate through all files in a folder and make simple calculation danielappleton 2 343 Apr-01-2020, 09:57 AM
Last Post: danielappleton

Forum Jump:

Users browsing this thread: 1 Guest(s)