Trying to create a text file with the date - python

I am trying to merge files and after create a new file and name it with the day date.
import datetime
import os
filename = datetime.datetime.now()
file1 = open("A1.txt", 'r+')
file2 = open("A2.txt", 'r+')
file3 = open("A3.txt", 'r+')
d1 = file1.read()
d2 = file2.read()
d3 = file3.read()
datac = [d1, d2, d3]
def file_w():
with open((filename.strftime("%D") + ".txt" ,'w+')) as file:
file.write()
for i in datac:
file.write(i)
file_w()

It looks like your error is the open statement (the parenthesis at near the end):
with open((filename.strftime("%D") + ".txt" ,'w+')) as file:
should be:
with open((filename.strftime("%D") + ".txt") ,'w+') as file:
but you might have a problem naming a file with "/" in it, such as generated by the "%D" in the strftime method. Instead you could try:
with open((filename.strftime("%m_%d_%Y") + ".txt") ,'w+') as file:

Related

Extract date from file name in python, not fixed name

I need to get date from file name in python code. I found many solutions, but from fixed name and date. But I dont know what the name of the file will be, date is changing. How to do that?
I have a code which is working for known file name (current date), file is called micro20230125.txt
import re
import os
from datetime import datetime
header = """#SANR0000013003;*;#CNR0010;*;#RINVAL-777.0;*;"""
current_timestamp = datetime.today().strftime('%Y%m%d')
input_file = "micro" + current_timestamp + ".txt"
output_file = os.path.splitext(input_file)[0] + ".zrxp"
with open(input_file, "r") as f:
first_line = f.readline().strip('\n')
text = re.search('(\d{6})', first_line).group(1)
text = header + "\n" + text + "\n"
with open(output_file, "w") as f:
f.write(text)
print(text)
`
but I dont need current date. I will get file with some random date, so how can I extract unknown date from file name? How to change this variable current_timestamp?
I tried to use regex but I messed something up
EDIT: DIFF CODE, SIMILAR PROBLEM:
I was dealing with this code and then realized: python doesnt know what those numbers in name represent, so why treat them like a date and complicate things? Those are just numbers. As a matter of fact, I need those numbers as long as full file name. So I came up with different code.
import re
import os
def get_numbers_from_filename(filename):
return re.search(r'\d+', filename).group(0) #returns only numbers
for filename in os.listdir("my path"):
print (get_numbers_from_filename(filename))
def get_numbers_from_filename(filename):
return re.search(r"(.)+", filename).group(0) #returns all name
for filename in os.listdir("my path"):
print(get_numbers_from_filename(filename))
file was: micro20230104.txt
and result is:
result
Now, I want to use that result, dont want to print it.
No matter how I get that returns me error.
import re
import os
def get_numbers_from_filename(filename):
return re.search(r"(.)+", filename).group(0)
for filename in os.listdir("my path"):
print(get_numbers_from_filename(filename))
m = get_numbers_from_filename(filename)
output_file = os.path.splitext(m)[0] + ".zrxp"
with open(m, "r") as f:
first_line = f.readline().strip('\n')
text = re.search('(\d{6})', first_line).group(1)
text = header + "\n" + text + "\n"
with open(output_file, "w") as f:
f.write(text)
print(text)
but it it says error
error:there is no such file
what to do? what am I doing wrong?
Well, in case all the files have the format 'micro[YearMonthDay].txt', you can try this solution:
import os
from datetime import datetime
header = """#SANR0000013003;*;#CNR0010;*;#RINVAL-777.0;*;"""
#Change the variable folder_path for your actual directory path.
folder_path = "\\path_files\\"
filenames = []
# Iterate directory
for path in os.listdir(folder_path):
# check if current path is a file
if os.path.isfile(os.path.join(folder_path, path)):
filenames.append(path)
dates = []
for filename in filenames:
# First solution:
filename = filename.replace('micro', '')
filename = filename.replace('.txt', '')
date = datetime.strptime(filename, "%Y%m%d")
# Second solution:
# date = datetime.strptime(filename, "micro%Y%m%d.txt")
dates.append(date)
for date in dates:
print(date.strftime("%Y/%m/%d"))
with open(f'.\\micro{date.strftime("%Y/%m/%d")}.txt', "r") as f:
first_line = f.readline().strip('\n')
text = re.search('(\d{6})', first_line).group(1)
text = header + "\n" + text + "\n"
with open(output_file, "w") as f:
f.write(text)
print(text)
Use the solution you prefer and comment the other one.
Testing:
Text files for test
Code
Result
I hope I could help! :D

Python: editing a series of txt files via loop

With Python I'm attempting to edit a series of text files to insert a series of strings. I can do so successfully with a single txt file. Here's my working code that appends messages before and after the main body within the txt file:
filenames = ['text_0.txt']
with open("text_0.txt", "w") as outfile:
for filename in filenames:
with open(filename) as infile:
header1 = "Message 1:"
lines = "\n\n\n\n"
header2 = "Message 2:"
contents = header1 + infile.read() + lines + header2
outfile.write(contents)
I'm seeking some assistance in structuring a script to iteratively make the same edits to a series of similar txt files in the directory. There are 20 or similar txt files are structured the same: text_0.txt, text_1.txt, text_2.txt, and so on. Any assistance is greatly appreciated.
to loop through a folder of text files, you need to do it like this:
import os
YOURDIRECTORY = "TextFilesAreHere" ##this is the folder where there's your text files
for file in os.listdir(YOURDIRECTORY):
filename = os.fsdecode(file)
with open(YOURDIRECTORY + "/" + filename, "r"):
###do what you want with the file
If you already know the file naming then you can simply loop:
filenames = [f'text_{index}.txt' for index in range(21)]
for file_name in filenames:
with open(file_name, "w") as outfile:
for filename in filenames:
with open(filename) as infile:
header1 = "Message 1:"
lines = "\n\n\n\n"
header2 = "Message 2:"
contents = header1 + infile.read() + lines + header2
outfile.write(contents)
Or loop the directory like:
import os
for filename in os.listdir(directory):
#do something , like check the filename in list

Python to search (text) files recursively and perform user-id deletion

OS: Ubuntu-18.04lts
Python version - 3.6.9
Excel report data.xlsx i have is,
I have many text files under /home/user/excel/report/directory and inside its sub-directories. Some of the text files along with path given below for reference.
/home/user/excel/report/file01.txt
/home/user/excel/report/folder-1/file02.txt
/home/user/excel/report/folder-1/filepath/file03.txt
/home/user/excel/report/folder-2/file04.txt
The filename of the text files are in excel sheet's B column. For each row, i need to a search the text file as per B column and need to look the User-ID in D column, if user-id exists in that particular row matched text file then user-id need to be removed from that text file, Same need to perform recursively.
Currently i below python code I'm using.
import os
import pandas as pd
data = pd.read_excel("data.xlsx")
d = dict(zip(data["File Name"], data["User-ID"]))
for file in d:
with open(f"/home/user/excel/report/" + file + ".txt", "r") as f:
contents = f.read().strip()
with open(f"/home/user/excel/report/" + file + ".txt", "w") as f:
f.write(contents.replace(d[file], ""))
Error:
$ python3.6 script.py
Traceback (most recent call last):
File "script.py", line 8, in <module>
with open(f"/home/user/excel/report/" + file + ".txt", "r") as f:
FileNotFoundError: [Errno 2] No such file or directory: '/home/user/excel/report/file03.txt'
Still script is look at /home/user/excel/report/directory only. Whereas file03.txt is present inside /home/user/excel/report/folder-1/filepath/ directory, Need help to fix this. Thanks.
You can use bash cmd find in python to find all the txt's paths.
import os
import pandas as pd
import subprocess
pycmd = lambda cmd: print(subprocess.check_output(cmd, shell=True, universal_newlines=True))
pycmd_output = lambda cmd: subprocess.check_output(cmd, shell=True, universal_newlines=True).strip().split('\n')
# use bash com find to find all txt file's path
os.chdir('/home/user/excel')
cmd = '''
find /home/user/excel/report/ -type f -name *.txt
'''
file_list = pycmd_output(cmd)
df_file_list = pd.Series(file_list)
file_list_name = df_file_list.str.split('/|\.').str[-2]
file_map = dict(zip(file_list_name ,df_file_list ))
# {'file02': '/home/user/excel/report/folder-1/file02.txt',
# 'file03': '/home/user/excel/report/folder-1/filepath/file03.txt',
# 'file04': '/home/user/excel/report/folder-2/file04.txt',
# 'file01': '/home/user/excel/report/file01.txt'}
data = pd.read_excel("data.xlsx")
data['file_path'] = data["File Name"].map(file_map)
# have duplicated values in `data["File Name"]`
d = data.groupby('file_path')['User-ID'].agg(list).to_dict()
for file, user_id_list in d.items():
with open(file, "r") as f:
contents = f.read().strip()
for user_id in user_id_list:
contents = contents.replace(user_id, "")
with open(file, "w") as f:
f.write(contents)
Considering
/home/user/excel/report/file01.txt
/home/user/excel/report/folder-1/file02.txt
/home/user/excel/report/folder-1/filepath/file03.txt
/home/user/excel/report/folder-2/file04.txt
you need first do discover where each file is located, if filenames are always unique this is relatively simple with os.walk. I would do:
import os
filepaths = {}
for dirpath, dirnames, filenames in os.walk("/home/user/excel/report"):
for fname in filenames:
filepaths[fname] = os.path.join(dirpath, fname)
print(filepaths)
which should created dict with keys being filenames and values paths to them. Then when you need to interact with file named "X" just use filepaths["X"].
If following code
import os
import pandas as pd
data = pd.read_excel("data.xlsx")
d = dict(zip(data["File Name"], data["User-ID"]))
for file in d:
with open(f"/home/user/excel/report/" + file + ".txt", "r") as f:
contents = f.read().strip()
with open(f"/home/user/excel/report/" + file + ".txt", "w") as f:
f.write(contents.replace(d[file], ""))
would work as intended if all files were inside /home/user/excel/report then following should work with files which might be in subdirs
import os
import pandas as pd
filepaths = {}
for dirpath, dirnames, filenames in os.walk("/home/user/excel/report"):
for fname in filenames:
filepaths[fname] = os.path.join(dirpath, fname)
data = pd.read_excel("data.xlsx")
d = dict(zip(data["File Name"], data["User-ID"]))
for file in d:
with open(filepaths[file+".txt"], "r") as f:
contents = f.read().strip()
with open(filepaths[file+".txt"], "w") as f:
f.write(contents.replace(d[file], ""))

Python - Read Multiple Files & Write To Multiple New Files

I know there's a lot of content about reading & writing out there, but I'm still not quite finding what I need specifically.
I have 5 files (i.e. in1.txt, in2.txt, in3.txt....), and I want to open/read, run the data through a function I have, and then output the new returned value to corresponding new files (i.e. out1.txt, out2.txt, out3.txt....)
I want to do this in one program run. I'm not sure how to write the loop to process all the numbered files in one run.
If you want them to be processed serially, you can use a for loop as follows:
inpPrefix = "in"
outPrefix = "out"
for i in range(1, 6):
inFile = inPrefix + str(i) + ".txt"
with open(inFile, 'r') as f:
fileLines = f.readlines()
# process content of each file
processedOutput = process(fileLines)
#write to file
outFile = outPrefix + str(i) + ".txt"
with open(outFile, 'w') as f:
f.write(processedOutput)
Note: This assumes that the input and output files are in the same directory as the script is in.
If you are looking just for running one by one separately you can do:
import os
count = 0
directory = "dir/where/your/files/are/"
for filename in os.listdir(directory):
if filename.endswith(".txt"):
count += 1
with open(directory + filename, "r") as read_file:
return_of_your_function = do_something_with_data()
with open(directory + count + filename, "w") as write_file:
write_file.write(return_of_your_function)
Here, you go! I would do something like this:
(Assuming all the input .txt files are in the same input folder)
input_path = '/path/to/input/folder/'
output_path = '/path/to/output/folder/'
for count in range(1,6):
input_file = input_path + 'in' + str(count) + '.txt'
output_file = output_path + 'out' + str(count) + '.txt'
with open(input_file, 'r') as f:
content = f.readlines()
output = process_input(content)
with open(output_file, 'w') as f:
w.write(output)

How to write data to a file in Python (File is create but no data)

When I pass the file name directly as below, data is being written to the output file.
Rpt_file_wfl = open('output.csv','a')
Rpt_file_wfl.write(output)
But when I pass the filename as a variable, the file is getting created but there is no data.
OUT_PATH = E:\MYDRIVE
outDir = py_script
outFiles = output.csv
Rpt_file_wfl = open(OUT_PATH+outDir+outFiles[0],'a')
Rpt_file_wfl.write(output)
I do close the file in the end.
Why would the data not be written with the above code.
Try to use os.path
import os
output_text = 'some text'
drive_path = 'E:'
drive_dir = 'Mydrive'
out_dir = 'py_script'
out_file = 'output.csv'
full_path = os.path.join(drive_path, drive_dir, out_dir, out_file)
with open(full_path, 'a', encoding='utf-8') as file:
file.write(output_text)
If it doesn't work - try to .replace() delimiters, like:
full_path = full_path.replace('/', '\\')
Or else:
full_path = full_path.replace('\\', '/')
Here`s example of working code:
OUT_PATH='D:\\output\\'
outDir='scripts\\'
outFiles=['1.csv', '2.csv']
path = OUT_PATH+outDir+outFiles[0]
output='Example output'
with open(path, 'a') as file:
file.write(output)

Categories

Resources