How would I read and write from multiple files in a single directory? Python -
How would I read and write from multiple files in a single directory? Python -
i writing python code , more insight on how approach issue.
i trying read in multiple files in order end .log. this, hope write specific values .csv file.
within text file, there x/y values extracted below:
textfile.log:
x/y = 5 x/y = 6 textfile.log.2:
x/y = 7 x/y = 8 desiredoutput in csv file:
5 6 7 8 here code i've come far:
def readfile(): import os = 0 file in os.listdir("\mydir"): if file.endswith(".log"): homecoming file def main (): import re list = [] list = readfile() line in readfile(): x = re.search(r'(?<=x/y = )\d+', line) if x: list.append(x.group()) else: break f = csv.write(open(output, "wb")) while 1: if (i>len(list-1)): break else: f.writerow(list(i)) += 1 if __name__ == '__main__': main() i'm confused on how create read .log file, .log.2 file. possible have automatically read files in 1 directory without typing them in individually?
update: i'm using windows 7 , python v2.7
the simplest way read files sequentially build list , loop on it. like:
for fname in list_of_files: open(fname, 'r') f: #do stuff each file this way whatever read each file repeated , applied every file in list_of_files. since lists ordered, occur in same order list sorted to.
borrowing @the2ndson's answer, can pick files os.listdir(dir). list files , directories within dir in arbitrary order. can pull out , order of files this:
allfiles = os.listdir(some_dir) logfiles = [fname fname in allfiles if "log" in fname.split('.')] logfiles.sort(key = lambda x: x.split('.')[-1]) logfiles[0], logfiles[-1] = logfiles[-1], logfiles[0] the above code work files name "somename.log", "somename.log.2" , on. can take logfiles , plug in list_of_files. note lastly line necessary if first file "somename.log" instead of "somename.log.1". if first file has number on end, exclude lastly step
line line explanation:
allfiles = os.listdir(some_dir) this line takes files , directories within some_dir , returns them list
logfiles = [fname fname in allfiles if "log" in fname.split('.')] perform list comprehension gather of files log in name part of extension. "something.log.somethingelse" included, "log_something.somethingelse" not.
logfiles.sort(key = lambda x: x.split('.')[-1]) sort list of log files in place lastly extension. x.split('.')[-1] splits file name list of period delimited values , takes lastly entry. if name "name.log.5", sorted "5". if name "name.log", sorted "log".
logfiles[0], logfiles[-1] = logfiles[-1], logfiles[0] swap first , lastly entries of list of log files. necessary because sorting operation set "name.log" lastly entry , "nane.log.1" first.
python file parsing csv
Comments
Post a Comment