Renaming log files with Python using wildcard and datetime - python

I was searching today for options to manipulate some log files, after executing actions on them, and I found that Python has the os.rename resource, after importing the os module, but I have some regex doubts..
Tried to fit a wildcard "*****" on my file names, but Python seems not to understand it.
My file names are:
Application_2021-08-06_hostname_[PID].log
Currently I'm asking Python to read these application files, and search for defined words/phrases such as "User has logged in", "User disconnected" and etc. And he does well. I'm using datetime module so Python will always read the current files only.
But what I'm trying to do now, is to change the name of the file, after Python read it and execute something. So when he find "Today's sessions are done", he will change the name of the file to:
Application_06-08-2021_hostname_[PID].log
Because it will be easier for manipulating later..
Considering that [PID] will always change, this is the part that I wanted to set the wildcard, because it can be 56, 142, 3356, 74567 or anything.
Using the os.rename module, I've got some errors. What do you suggest?
Code:
import os
import time
from datetime import datetime
path = '/users/application/logs'
file_name = 'Application_%s_hostname_'% datetime.now().strftime('%Y-%m-%d')
new_file_name = 'Application_%s_hostname_'% datetime.now().strftime('%d-%m-%Y')
os.rename(file_name, new_file_name)
The error is:
OSError: [Errno 2] No such file or directory

You can use glob which allows for wildcards:
import glob, os
from datetime import datetime
current_date = datetime.now()
path = '/users/application/logs'
# note the use of * as wild card
filename ='Application_%s_hostname_*'% current_date.strftime('%Y-%m-%d')
full_path = os.path.join(path, filename)
replace_file = glob.glob(full_path)[0] # will return a list so take first element
# or loop to get all files
new_name = replace_file.replace( current_date.strftime('%Y-%m-%d'), current_date.strftime('%d-%m-%Y') )
os.rename(replace_file, new_name)

Related

Finding a file in the same directory as the python script (txt file)

file = open(r"C:\Users\MyUsername\Desktop\PythonCode\configure.txt")
Right now this is what im using. However if people download the program on their computer the file wont link because its a specific path. How would i be able to link the file if its in the same folder as the script.
You can use __file__. Technically not every module has this attribute, but if you're not loading your module from a file, loading a text file from the same folder becomes a moot point anyway.
from os.path import abspath, dirname, join
with open(join(dirname(abspath(__file__)), 'configure.txt')):
...
While this will do what you're asking for, it's not necessarily the best way to store configuration.
Use os module to get your current filepath.
import os
this_dir, this_filename = os.path.split(__file__)
myfile = os.path.join(this_dir, 'configure.txt')
file = open(myfile)
# import the OS library
import os
# create the absolute location with correct OS path separator to file
config_file = os.getcwd() + os.path.sep + "configure.txt"
# Open file
file = open(config_file)
This method will make sure the correct path separators are used.

Write a Python directory path string that contains both single and double back slash?

I know python prints strings containing backslash differently from the way it actually is. I do not care about print statements but I just want the string to begin with two back slashes and all the other slashes to be one slash like this:
\\xyz\abc\fish\orange
Currently, this is what I have coded and I get the error below:
import shutil
dir_path = r"\\xyz\abc\fish\orange"
print(dir_path)
shutil.copyfile("U:/Profile/Downloads/document1", dir_path)
I get an error:
FileNotFoundError: [Errno 2] No such file or directory: '\\\\xyz\\abc\\fish\\orange'
I replaced the actual directory name with mock names but what's important is formatting the slashes. I would prefer to fix this issue while not using os library. Thank you.
You can do like this.
import shutil
from pathlib import Path
dir_path = Path(r"\xyz\abc\fish\orange\document1") print(dir_path)
if dir_path.exist():
shutil.copyfile("U:/Profile/Downloads/document1", dir_path)
else:
print(f"{dir_path} do not exist")
You can also catch the error.
import shutil
dir_path = r"\xyz\abc\fish\orange\document1" print(dir_path)
try:
shutil.copyfile("U:/Profile/Downloads/document1", dir_path)
except FileNotFoundError:
print(f"{dir_path} do not exist")

Renaming file extension using pathlib (python 3)

I am using windows 10 and winpython. I have a file with a .dwt extension (it is a text file). I want to change the extension of this file to .txt.
My code does not throw any errors, but it does not change the extension.
from pathlib import Path
filename = Path("E:\\seaborn_plot\\x.dwt")
print(filename)
filename_replace_ext = filename.with_suffix('.txt')
print(filename_replace_ext)
Expected results are printed out (as shown below) in winpython's ipython window output:
E:\seaborn_plot\x.dwt
E:\seaborn_plot\x.txt
But when I look for a file with a renamed extension, the extension has not been changed, only the original file exists. I suspect windows file permissions.
You have to actually rename the file not just print out the new name.
Use Path.rename()
from pathlib import Path
my_file = Path("E:\\seaborn_plot\\x.dwt")
my_file.rename(my_file.with_suffix('.txt'))
Note: To replace the target if it exists use Path.replace()
Use os.rename()
import os
my_file = 'E:\\seaborn_plot\\x.dwt'
new_ext = '.txt'
# Gets my_file minus the extension
name_without_ext = os.path.splitext(my_file)[0]
os.rename(my_file, name_without_ext + new_ext)
Ref:
os.path.splitext(path)
PurePath.with_suffix(suffix)
From the docs:
Path.rename(target)
Rename this file or directory to the given target. On Unix, if target exists and is a file, it will be replaced silently if the user has permission. target can be either a string or another path object.
pathlib — Object-oriented filesystem paths on docs.python.org
You could use it like this:
from pathlib import Path
filename = Path("E:\\seaborn_plot\\x.dwt")
filename_replace_ext = filename.with_suffix(".txt")
filename.rename(filename_replace_ext)

how to convert variable module shelve?

please help convert the variable "fileNameClean" so that you can open the file via the module "shelve"
import shelve
fileName = 'C:/Python33/projects/DVD_LIST/p3_dvd_list_shelve_3d_class_edit_menubar/data.dir'
print('___', fileName)
str = fileName.split('/')[-1]
print('--', str)
fileNameClean = str.split('.')[0:-1]
print(fileNameClean) #['data']
db = shelve.open(fileNameClean) #open error
Use the os.path module to produce a clean path:
import os.path
fileName = 'C:/Python33/projects/DVD_LIST/p3_dvd_list_shelve_3d_class_edit_menubar/data.dir'
fileNameClean = os.path.splitext(os.path.basename(fileName))[0]
db = shelve.open(fileNameClean)
Your code also produced a base name minus the extension, but you returned a list by using a slice (from index 0 until but not including the last element). You could have used fileNameClean[0], but using the os.path module makes sure you catch edgecases in path handling too.
You probably want to make sure you don't use a relative path here either. To open the shelve file in the same directory as the current script or module, use __file__ to get an absolute path to the parent directory:
here = os.path.dirname(os.path.abspath(__file__))
db = shelve.open(os.path.join(here, fileNameClean))

Reading files based on file name and not data type

I am very new to python. I want to read files based on file name and not data type. Say I have Hello.txt_1, Hello.txt_2, Hello.txt_3 in a folder and these files are created automatically by an external code with Hello.txt_3 being the latest file. Now, I want to read the latest created file Hello.txt_3 and check for its contents. How is to be done in python ? I have figured out for files with common data type but not for common file name.
Use glob to perform wildcard matching. The following example will find all the files named as you state and last_file will contain the name of the latest by creation time (or None if no files were found).
import glob
import os
last_file = None
time=0
for i in glob.glob("Hello.txt_*"):
if os.path.getctime(i) > time:
last_file = i
PS: This question is at the very beginner level however and should have been easily solved by googling.
From my understanding of the question, you probably want to ignore the file types as you cannot depend on them to tell you what you want to know.
If the file type contains a number/alpha-numeric, 'sorted' will sort it in reverse order. Then you can simple read the first:
#!/usr/bin/python
from os import listdir
from os.path import isfile, join
#this is where you declare you directory
directory_2_look = '/var/tmp/lee/'
#This creates a list of the contents of your directory
files = [ f for f in listdir(directory_2_look) if isfile(join(directory_2_look,f)) ]
#this sorts the files for you in reverse order
files = sorted(files, reverse=True)
print files[0]
#this allows you to read the last file and print it out. If you want to write to it simply change the 'r'
file_to_read = open(join(directory_2_look,files[0]), 'r')
print file_to_read.read()
Results will look a bit like this:
['script.py', 'file.txt_99', 'file.txt_4', 'file.txt_3',
'file.txt_22', 'file.txt_21', 'file.txt_2', 'file.txt_1',
'file.txt_0'] script.py
!/usr/bin/python from os import listdir from os.path import isfile, join directory_2_look = '/var/tmp/lee/' files = [ f for f in
listdir(directory_2_look) if isfile(join(directory_2_look,f)) ] print
sorted(files, reverse=True) files = sorted(files, reverse=True) print
files[0] file_to_read = open(join(directory_2_look,files[0]), 'r')
print file_to_read.read()

Categories

Resources