I am taking zip file as input which contains multiple files and folders,I am extracting it and then I want to change the last modified time of each content in zip to some new date and time set by user.
I am using os.utime() to change the date and time, but changes get reflected only to the files and not to the folders inside zip.
timeInStr = raw_input("Enter the new time =format: dd-mm-yyyy HH:MM:SS -")
timeInDt=datetime.datetime.strptime(timeInStr, '%d-%m-%Y %H:%M:%S')
timeInTS=mktime(timeInDt.timetuple())
epochTime=(datetime.datetime(timeInDt.year, timeInDt.month, timeInDt.day, timeInDt.hour, timeInDt.minute, timeInDt.second)-datetime.datetime(1970,1,1)).total_seconds()
z=zp.ZipFile(inputZipFile,"a",zp.ZIP_DEFLATED)
for files in z.infolist():
z.extract(files, srcFolderName)
fileName=files.filename
new= fileName.replace('/',os.path.sep)
correctName= srcFolderName+os.path.sep+new
print correctName
if(correctName.endswith(os.path.sep)):
correc=correctName[:-1]
print correc
os.utime(correc, (timeInTS, timeInTS))
else:
os.utime(correctName, (timeInTS, timeInTS))
I am using Python 2.7 as platform
Base to the directory permission is this question on SO. The directory only changes its timestamp when the directory itself changes for ex: when you create a new file in it. So to update the timestamp of folder you can create a temp file and then delete it. There should be a better way but till you find it you can manage using this.
I ran into a similar problem. Here is the code I used to get past the issue.
As user966588 stated, the directory's timestamp is updating as the directory changes.
In the post I linked, I held onto any directory metadata updates until after my directory was fully-populated in order for the timestamp change to stay.
Related
I am in the process of making an automated script. Basically I am downloading some shapefiles, unzipping them and them making a few changes each month. Each month I download the same dataset.
An issue I have found is that the dataset name changes each month after I download it, I'm not sure how i can point the script too it if the name changes? I don't really want to have to update the script with the new file path each month.
For example November was
L:\load\Ten\Nov20\NSW\FME_68185551_1604301077137_7108\GSNSWDataset
And Dec is
L:\load\Ten\Dec20\NSW\FME_68185551_1606880934716_1252\GSNSWDataset
You could use glob with a wildcard in the changing number section. Something like:
import glob
import datetime
d = datetime.today().strftime('%b%y') #'Dec20'
fil = glob.glob("L:/load/Ten/%s/NSW/FME*/GSNSWDataset" % (d))[0]
This should get you the correct path to your files and then you just read/manipulate however you need.
I created a tool that go through certain path using os.walk to check the folder if it's empty or not and list down the folders and files inside it if there is any this is how the result is
...\1.This folder\My Folder\Recored
['My Text 1.txt', 'My Text 2.txt']
OR
...\1.My Pic
This Folder is empty :(
what I want to do is track changes and color with red the new folders or the files that has been modified since the last run.
I don't want to keep watching the changes I want to see what has been change since the last run
I was trying to have something in text like log so I can compare between the current list and the text with no success
for path, directory, files in os.walk(r'C:\Users\J\MyFolder'):
if files or directory:
print(path)
print("\n")
print((os.listdir(path)))
Folder modified timestamp (os.path.getmtime("folder_path")) will provide information about folder changes timestamp value.
Your system (tool which you have created) can tell when it ran previously and also get the timestamp of folder which you are scanning, compare and color it accordingly.
To get changes in datetime format;
datetime.fromtimestamp(os.path.getmtime(folder_path))
I have a .pd file called 'missing' on my computer. The path is C:\me\Desktop\missing.pd
Inside this file there is just dates. I have an algo which create and populate this 'missing.pd' with dates everytime I run it. My algo basically create a dataframe with inside some dates, sometime empty and then create the missing.pd file on my computer and add the dates.
What I am trying to do is to not recreate everytime the missing.pd file (that's what my code do so far).
I want to say to my code :
if C:\me\Desktop\missing.pd exist, then check inside if the dates of my created dataframe are already here, if no add the ones which are not already here, if missing.pd do not exist, create it and fill it with the dates.
so far for this part of the code, it is :
path = r"C:\me\Desktop\missing.pd"
missing = pd.DataFrame(missing)
missing.to_pickle( os.path.join(path,"%s_missing.pd"%(country)))
You can use os.path.isfile(filename) to check if the file exists. Documentation here.
import os.path
if os.path.isfile(path):
"""Your date checking code here."""
I'm trying to code a script in python that creates a folder entitled the current timestamp, but don't really know the syntax to do it. So far I have this:
timestamp = int(time.time()) #fetches timestamp
if not os.path.exists([timestamp]): #creates new folder titled the current timestamp
os.makedirs([timestamp])
os.chdir([timestamp]) #navigates to new folder
When I run this, I run into errors.
P.S. I am a beginner, please don't bite.
The timestamp needs to be converted to a string. and then that string need to be passed as the argument to the os functions:
timestamp = str(int(time.time())) #fetches timestamp
if not os.path.exists(timestamp): #creates new folder titled the current timestamp
os.makedirs(timestamp)
os.chdir(timestamp) #navigates to new folder
I have a folder containing various files which are named using timestamps like this:
2016.08.06_09.31.53_test
2016.08.06_09.36.23_test1
2016.08.04_10.41.23_test
2016.08.04_10.46.20_test1
I am trying to write a program that can pull out only files from this folder which have a time stamp within a certain time span.
E.g. if the user Input is a Folder Directory and the timespan is 6.8.2016, 9am to 10 pm and the program should give back only the first two files from above.
What is the best way to do so?
With the above naming system, it is incredibly simple... Get your files into a list
list_of_files = [file1, file2 , ... ]
list_of_files.sort()
print list_of_files
That will sort it for you incredibly quickly - the .sort() changes the original list, and puts it into alphabetical order (which here is all you need)