I have text files that are named in the following way:
I would like to create a new file with all the content of the files that have the same number before the "_". In this instance, the new file should be 301.txt. What is the best way to do this in Python?
Thank you
This is my approach: (hope it helps :) )
Firstly, we need to store the files starting with "301":
import os
files = []
directory = os.fsencode(directory of files)
for file in os.listdir(directory):
filename = os.fsdecode(file)
if filename.startswith("301"):
files.append(filename)
The directory of files is the directory of where your files go.
Now we need to fetch the data from the files:
data = []
for f in files:
with open(f) as file:
for i,row in enumerate(file):
data.append(row)
Finally we need to write the "301.txt" file:
with open("301.txt",'w') as newfile:
for row in data:
newfile.write(row)
You could so something like this.
# populated by os.listdir or something
FILES = ['301_1.txt', '301_2.txt', '301_3.txt', '302_1.txt', '302_2.txt']
def concat_files(base='301'):
with open(base + '.txt', 'a+') as basefile:
for file in [f for f in FILES if f.startswith(base + '_')]:
with open(file) as content:
basefile.write(content.read())
concat_files(base='301')
import glob
import os
pwd=os.getcwd()
os.chdir('path_to_your_directory')
for i in glob.glob('*.txt'):
new_name=i.split('_')[0] #fetch the name before '_'
write_file=open(new_name+'.txt','a') #open file in append mode
read_file=open(i)
lines=read_file.read()
write_file.write(lines)
write_file.close()
read_file.close() #close the files
os.chdir(pwd)
glob.glob('*.txt')
will return a list of all files with extension .txt
in the current directory. First, we store the present directory in pwd , then to get to the current directory we use os.chdir()
from os
module and at last, we return to our starting directory by os.chdir(pwd)
. If you don't want newline characters then use read_file.read().rstrip()
instead.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.