My current python script:
import ftplib
import hashlib
import httplib
import pytz
from datetime import datetime
import urllib
from pytz import timezone
import os.path, time
import glob
def ftphttp():
ts = os.path.getmtime('Desktop/images/frame00.png')
dt = datetime.fromtimestamp(ts, pytz.utc)
timeZone= timezone('Asia/Singapore')
#converting the timestamp in ISOdatetime format
localtime = dt.astimezone(timeZone).isoformat()
cam = "002"
lscam = localtime + cam
ftp = ftplib.FTP('localhost','kevin403','S$ip1234')
ftp.cwd('/var/www/html/image')
m=hashlib.md5()
m.update(lscam)
dd=m.hexdigest()
for image in glob.glob(os.path.join('Desktop/images/frame**.png')):
with open(image, 'rb') as file:
ftp.storbinary('STOR '+dd+ '.png', file)
x = httplib.HTTPConnection('localhost', 8086)
x.connect()
f = {'ts' : localtime}
x.request('GET','/camera/store?cam='+cam+'&'+urllib.urlencode(f)+'&fn='+dd)
y = x.getresponse()
z=y.read()
x.close()
ftp.quit()
As right now this line of code is only get one file timestamp:
ts = os.path.getmtime('Desktop/images/frame00.png').
But what if i send multiple file from a folder and get all the files timestamp. Is it possible to do it? I using ftplib to send multiple from a folder to another folder.
You could just walk the directory and getmtime for all the files in that directory if the directory in question is Desktop/images
You can replace
ts = os.path.getmtime('Desktop/images/frame00.png')
dt = datetime.fromtimestamp(ts, pytz.utc)
With something like:
dirToCheck = 'Desktop/images'
for root, _, file in os.walk(dirToCheck):
fileToCheck = os.path.join(root, file)
ts = os.path.getmtime(fileToCheck)
dt = datetime.fromtimestamp(ts, pytz.utc)
print ts, dt
With this approach you will need to give the absolute path to the images directory and also maybe store the timestampes in a list or dictionary with file - timestamp.
If you want to do it for png only you can also add a line like:
if file.endswith(".png"):
before the fileToCheck line.
This should do it unless i misunderstood your question.
Related
I have a csv file in downloads folder with a name export.csv.
I need to rename the file in the format "REMSIMPORT + today's date" and move to destination folder.
For e.g. today's date is 2022-01-24. So export.csv will be renamed as REMSIMPORT20220124.
Tomorrow it will be REMSIMPORT20220125.
So it will change every time as per current date
This is how far I have reached.
from datetime import date
src_path = r'C:/Users/abc/Downloads/'
dest_path = r"C:\Users\abc\Desktop\Weekly"
today_date = date.today() # Extracted today's date
today_date = str(today_date).replace("-","")
I am confused on how to rename file with a variable newname (e.g. REMSIMPORT20220124) and moving to destination folder.
Use the rename method from os module.
import os
src_path = r'C:/Users/abc/Downloads/' + 'REMSIMPORT'
dest_path = r'C:/Users/abc/Desktop/Weekly/' + 'REMSIMPORT' + today_date
os.rename(src_path, dest_path)
You may use schedule for your purpose.
$ pip install schedule
Here is some examples of schedule from the page :
import schedule
import time
def job():
print("I'm working...")
schedule.every(10).seconds.do(job)
schedule.every(10).minutes.do(job)
schedule.every().hour.do(job)
schedule.every().day.at("10:30").do(job)
schedule.every(5).to(10).minutes.do(job)
schedule.every().monday.do(job)
schedule.every().wednesday.at("13:15").do(job)
schedule.every().minute.at(":17").do(job)
while True:
schedule.run_pending()
time.sleep(1)
On your purpose, you can try something like this :
from datetime import date
import pandas as pd
import schedule
import time
def save_csv():
today_date = str(date.today()).replace("-","")
src_path = r'C:/Users/abc/Downloads/'
dest_path = r'C:\Users\abc\Desktop\Weekly'
df = pd.read_csv(src_path + 'export.csv')
df.to_csv(dest_path + 'REMSIMPORT' + today_date + '.csv')
schedule.every().day.at("10:30").do(save_csv)
while True:
schedule.run_pending()
time.sleep(1)
Shutil copyfile should be good!
from datetime import date
from shutil import copyfile
src_path = r'C:/Users/user/adifferentdir'
dest_path = r"C:/Users/user/dir"
today_date = date.today() # Extracted today's date
today_date = str(today_date).replace("-","")
copyfile(src_path+"/export.csv",dest_path+"/REMSIMPORT"+today_date+".csv")#copying file with new name```
I think using the shutil.move() utility function would be the best option because not only can it move files, it can rename them at the same time. I've also found that using the pathlib module makes file and directory path manipulations not only extremely easy but at very intuitive (and allow doing so in an OS portable way).
Anyway, here's an example of putting them to use to accomplish what you want to do.
from datetime import date
from pathlib import Path
from shutil import move
filename = 'export.csv'
#src_dir = Path('C:/Users/abc/Downloads/')
#dst_dir = Path('C:/Users/abc/Desktop/Weekly')
# Directories for testing.
src_dir = Path('./').resolve()
dst_dir = Path('./Weekly').resolve()
prefix = 'REMSIMPORT'
today_date = date.today() # Extracted today's date
today_date = str(today_date).replace('-', '')
newname = prefix + today_date
if not dst_dir.exists():
dst_dir.mkdir() # Create destination directory since it doesn't exist.
src_path = src_dir / filename
dst_path = dst_dir / (newname + src_path.suffix)
move(src_path, dst_path)
print('fini')
I am trying to validate the filenames which are having datepart in their name. What i am trying to do is i want to check a filename with specified format where datpart is different for different file. If the filename doesn't match it should give us the message file not found.
Filename e.g =abc_sales_2020-09-01_exp.csv, abc_sales_2020-09-02_exp.csv,abc_sales_2020-09-03_exp.csv. Only the datepart changes rest remains the same.
from datetime import date
def get_filename_datetime():
return "ak_sales_" + str(date.today()) + "_abc"+".csv"
name = get_filename_datetime() print("NAME", name) path = "aks/" +
name print("PATH", path);
with open(path, "r") as f:
f.read()
You can use regex to filter specific files and then open them with pandas:
import re
import glob
import pandas as pd
for file in glob.glob('*.csv'):
r = re.findall('\d\d\d\d-\d\d-\d\d', file)
if r:
df = pd.read_csv(file)
I'm trying to extract files from a zip file using Python 2.7.1 (on Windows, fyi) and each of my attempts shows extracted files with Modified Date = time of extraction (which is incorrect).
import os,zipfile
outDirectory = 'C:\\_TEMP\\'
inFile = 'test.zip'
fh = open(os.path.join(outDirectory,inFile),'rb')
z = zipfile.ZipFile(fh)
for name in z.namelist():
z.extract(name,outDirectory)
fh.close()
I also tried using the .extractall method, with the same results.
import os,zipfile
outDirectory = 'C:\\_TEMP\\'
inFile = 'test.zip'
zFile = zipfile.ZipFile(os.path.join(outDirectory,inFile))
zFile.extractall(outDirectory)
Can anyone tell me what I'm doing wrong?
I'd like to think this is possible without having to post-correct the modified time per How do I change the file creation date of a Windows file?.
Well, it does take a little post-processing, but it's not that bad:
import os
import zipfile
import time
outDirectory = 'C:\\TEMP\\'
inFile = 'test.zip'
fh = open(os.path.join(outDirectory,inFile),'rb')
z = zipfile.ZipFile(fh)
for f in z.infolist():
name, date_time = f.filename, f.date_time
name = os.path.join(outDirectory, name)
with open(name, 'wb') as outFile:
outFile.write(z.open(f).read())
date_time = time.mktime(date_time + (0, 0, -1))
os.utime(name, (date_time, date_time))
Okay, maybe it is that bad.
Based on Jia103's answer, I have developed a function (using Python 2.7.14) which preserves directory and file dates AFTER everything has been extracted. This isolates any ugliness in the function, and you can also use zipfile.Zipfile.extractAll() or whatever zip extract method you want:
import time
import zipfile
import os
# Restores the timestamps of zipfile contents.
def RestoreTimestampsOfZipContents(zipname, extract_dir):
for f in zipfile.ZipFile(zipname, 'r').infolist():
# path to this extracted f-item
fullpath = os.path.join(extract_dir, f.filename)
# still need to adjust the dt o/w item will have the current dt
date_time = time.mktime(f.date_time + (0, 0, -1))
# update dt
os.utime(fullpath, (date_time, date_time))
To preserve dates, just call this function after your extract is done.
Here's an example, from a script I wrote to zip/unzip game save directories:
z = zipfile.ZipFile(zipname, 'r')
print 'I have opened zipfile %s, ready to extract into %s' \
% (zipname, gamedir)
try: os.makedirs(gamedir)
except: pass # Most of the time dir already exists
z.extractall(gamedir)
RestoreTimestampsOfZipContents(zipname, gamedir) #<-- USED
print '%s zip extract done' % GameName[game]
Thanks everyone for your previous answers!
Based on Ethan Fuman's answer, I have developed this version (using Python 2.6.6) which is a little more consise:
zf = ZipFile('archive.zip', 'r')
for zi in zf.infolist():
zf.extract(zi)
date_time = time.mktime(zi.date_time + (0, 0, -1))
os.utime(zi.filename, (date_time, date_time))
zf.close()
This extracts to the current working directory and uses the ZipFile.extract() method to write the data instead of creating the file itself.
Based on Ber's answer, I have developed this version (using Python 2.7.11), which also accounts for directory mod dates.
from os import path, utime
from sys import exit
from time import mktime
from zipfile import ZipFile
def unzip(zipfile, outDirectory):
dirs = {}
with ZipFile(zipfile, 'r') as z:
for f in z.infolist():
name, date_time = f.filename, f.date_time
name = path.join(outDirectory, name)
z.extract(f, outDirectory)
# still need to adjust the dt o/w item will have the current dt
date_time = mktime(f.date_time + (0, 0, -1))
if (path.isdir(name)):
# changes to dir dt will have no effect right now since files are
# being created inside of it; hold the dt and apply it later
dirs[name] = date_time
else:
utime(name, (date_time, date_time))
# done creating files, now update dir dt
for name in dirs:
date_time = dirs[name]
utime(name, (date_time, date_time))
if __name__ == "__main__":
unzip('archive.zip', 'out')
exit(0)
Since directories are being modified as the extracted files are being created inside them, there appears to be no point in setting their dates with os.utime until after the extraction has completed, so this version caches the directory names and their timestamps till the very end.
I have the following issue, I wrote a piece of code which rename file names in a directory and its sub-directories. Now instead of changing it to the current date I want it to change to the file modification date.
How can I do that?
import os, path
from datetime import datetime
import time
def walk_dir(path):
current_day = datetime.now().strftime("%Y-%m-%d")
for root, dirs, files in os.walk(path):
for filename in files:
current = root + "/" + filename
if os.path.isfile(current):
print "ORIGINAL NAME: " + current
ext = os.path.splitext(filename)[1]
target_name = os.path.join(root, '{}{}'.format(current_day, ext))
print "NEW NAME: " + target_name
os.rename(current, target_name)
walk_dir("/Users/shirin/Desktop/Artez")
import os
import datetime
for filename in directory:
modified_time = os.path.getmtime(filename) # get file modification timestamp
modified_date = datetime.date.fromtimestamp(modified_time) # convert timestamp to a date
os.rename(filename, modified_date.strftime("%Y-%m-%d"))
Note this is dangerous, since you might override files who share the same modification date.
I have a list of files with names such as these:
20140911_085234.csv
20140912_040056.csv
What is known is the first part which is the date (the second is a random number). How can I open the correct file if I know the date?
Update: There is one file per day.
As #isedev says, you could use the fnmatch method to find all the files with the "date" pattern. The code could be like this:
from fnmatch import fnmatch
import os
folder_path = '/home/Desktop/project'
all_files = os.listdir(folder_path)
content_file = 'Hello World'
_date = '20140911'
_pattern = _date + '*'
for file_name in all_files:
if fnmatch(file_name, _pattern):
with open(os.path.join(folder_path, file_name), 'wb') as f:
f.write(content_file)
I hope it helps you!
Using glob :
import time
import glob
import os
def open_file_by_date(date):
path = "/path/to/file"
files = glob.glob1(path, date + "_*.csv")
for file in files:
with open(os.path.join(path, file), 'wb') as f:
#do your stuff with file
if __name__ == "__main__":
today = time.strftime("%Y%m%d")
open_file_by_date(today)