Python Forum
search and read file given partial file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
search and read file given partial file
#1
Hi,
I want to read file given a partial file name:

import glob
my_dir = 'D:\PythonCodes'
#post_fix = ['\InputFiles\KYLJHQ_20191215\sub_folder/InputCSV.csv','\InputFiles\KYLJHQ_20191215\sub_folder/InputCSV2.csv']
post_fix = ['\InputFiles\KYLJHQ_*\sub_folder/InputCSV.csv','\InputFiles\KYLJHQ_*\sub_folder/InputCSV2.csv']


for i in range(len(post_fix)):
    full_path = glob.glob(my_dir + str(post_fix[i]))
    tmp_file = open(full_path,'r')
    tmp_data = tmp.readlines()
    print(tmp_data)
the file structure is D:\PythonCodes -->\InputFiles\KYLJHQ_*\sub_folder/InputCSV.csv. I uae above code but it giving error:

TypeError: expected str, bytes or os.PathLike object, not list
Reply
#2
this is where functions come in.
def read_file(filaneme):
    with open(filename) as fp:
        for line in fp:
            fp.strip()
            ...
Reply
#3
You give a list to open in line 9.
Some point you don't need glob as you construct a full path here,and use join to join paths.
So if put it together and use also code from Larz.
from os.path import join

def read_file(filename):
    with open(filename) as fp:
        for line in fp:
            print(line.strip())

# No \ singel in path,eg use raw string
my_dir = r'D:\PythonCodes'
post_fix = ['InputFiles\KYLJHQ_*\sub_folder\InputCSV.csv', 'InputFiles\KYLJHQ_*\sub_folder\InputCSV2.csv']
full_path = []
for r_path in post_fix:
    full_path.append(join(my_dir, r_path))

# Now read both files
for path in full_path:
    read_file(path)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to write variable in a python file then import it in another python file? tatahuft 4 876 Jan-01-2025, 12:18 AM
Last Post: Skaperen
  How to read a file as binary or hex "string" so that I can do regex search? tatahuft 3 1,006 Dec-19-2024, 11:57 AM
Last Post: snippsat
  Search in a file using regular expressions ADELE80 2 668 Dec-18-2024, 12:29 PM
Last Post: ADELE80
  Read TXT file in Pandas and save to Parquet zinho 2 1,206 Sep-15-2024, 06:14 PM
Last Post: zinho
  JSON File - extract only the data in a nested array for CSV file shwfgd 2 1,033 Aug-26-2024, 10:14 PM
Last Post: shwfgd
  FileNotFoundError: [Errno 2] No such file or directory although the file exists Arnibandyo 0 835 Aug-12-2024, 09:11 AM
Last Post: Arnibandyo
  Pycharm can't read file Genericgamemaker 5 1,544 Jul-24-2024, 08:10 PM
Last Post: deanhystad
  Python is unable to read file Genericgamemaker 13 3,581 Jul-19-2024, 06:42 PM
Last Post: snippsat
  "[Errno 2] No such file or directory" (.py file) IbrahimBennani 13 6,185 Jun-17-2024, 12:26 AM
Last Post: AdamHensley
  Delete file with read-only permission, but write permission to parent folder cubei 6 25,297 Jun-01-2024, 07:22 AM
Last Post: Eleanorreo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020