Aug-20-2021, 08:12 AM
(Aug-20-2021, 05:48 AM)AlphaInc Wrote:(Aug-19-2021, 10:12 PM)snippsat Wrote: On Windows the home path will beC:\Users\<username>\OutputsYou can give path to where you have the .txt files,if not want to make this Outputs folder.
Eg.
outputs = Path(r'G:\div_code\answer')Test and i write code to save lines,as redirect as show bye DeaD_EyE may not work for you as eg gzip,zcat is not a part of Windows.
from pathlib import Path def sort_by_int(path): # Path has the stem attribute, which is # the filename without the last extension # to sort the paths by integer, you # need to get the integer part of the str # and convert it to an integer # the _ is the character where you can split # maxsplit=1 does only split once, # so you get two elements back # if the _ is missing, split will raise an Exception return int(path.stem.split("_", maxsplit=1)[1]) # Use the high level Path object #outputs = Path.home() / "Outputs" outputs = Path(r'G:\div_code\answer') #print(outputs) # Path: /home/username/Outputs # use glob for easier search # rglob is to search recursive # glob and rglob replicates the shell-syntax # the wildcard is one * and a ? stands for one character search = "file_*.txt" # sorted takes a key argument, which is used to define how it's sorted # sort_by_int just returns an int and the sorted function # is using this number to sort sorted_outputs = sorted(outputs.glob(search), key=sort_by_int) #print(sorted_outputs) # the result is a list # sorted consumes the iterable object and returns a list # with the sorted elements # now using this sorted list with Path objects lines = [] for path in sorted_outputs: # glob does not differe between files, directories or other # elements # so you need to check, if path is a file if path.is_file(): print(path) # if it's a file, then open it # the Path onject do have the method open # it supports like the open function a context manager with path.open() as fd: # iterating this file line by line # where the line end is not stripped away for line in fd: # print the line, but tell print not to add an additional # line end, because the line has already a line end print(line) lines.append(line.strip()) #zf.writestr(str(path), f) # you can use the stdout to redirect the output # in your shell to a file for example or netcat # or gzip etc... with open('lines.txt', 'w') as f: f.write('\n'.join(lines))lines.txt:
Output:G:\div_code\answer\file_1.txt line1 G:\div_code\answer\file_3.txt line2 G:\div_code\answer\file_10.txt line3 G:\div_code\answer\file_33.txt line4
Output:line1 line2 line3 line4
Thanks also for your help. I can see that the Script process all my files but it does not create an output-file
Edit: My Bad, just looked after the wrong name. I found it. Thank you.
Sorry once again but I get an error:
Traceback (most recent call last): File "FileProcessing_11.py", line 30, in <module> sorted_outputs = sorted(outputs.glob(search), key=sort_by_int) File "FileProcessing_11.py", line 13, in sort_by_int return int(path.stem.split("_", maxsplit=1)[1]) IndexError: list index out of rangeCould it be because the files got spaces in it?