Get the update date of the Python memo file.

os.stat I'm writing about getting time here, but I'll write a little more. Python memorandum: Refer to the text and edit the file name while copying the target file

>>> os.stat(file_path).st_mtime
>>> 1442209206.0

You can get the changed time. But this is only a value.

datetime.datetime.fromtimestamp Convert with datetime.datetime.fromtimestamp.

>>> datetime.datetime.fromtimestamp(os.stat(file_path).st_mtime)
datetime.datetime(2015, 9, 14, 14, 40, 6)

dt.strftime If you convert it to a character string with dt.strftime, it's done.

>>> dt.strftime('%Y/%m/%d  %H:%M:%S')
'2015/09/14  14:40:06'

Export the date of the target file from the path to other text information.

gettime.py


# -*- coding: shift-jis -*-
def main():
    pass

if __name__ == '__main__':
    main()

import os
import datetime
from datetime import datetime as dt

os.chdir(r"D:\temp")

txt_file = open(r"D:\temp\photo.txt","r")

for target_line in txt_file:
    if target_line == '\n':
        break

    target = target_line[:-1]
    file_name = os.path.basename(target_line)[:-1]

    print file_name

    if os.path.exists(target) :
        dt = datetime.datetime.fromtimestamp(os.stat(target).st_mtime)
        key = dt.strftime('%Y/%m/%d  %H:%M:%S')
        txt_file_out = open(r"D:\python\py_date\ipad_img_file_date.csv", "a+")
        txt_file_out.write(target + "," + file_name + "," + str(dt) + "," + key + "\n")

txt_file_out.close()
txt_file.close()

It looks similar in Excel.

More on that later.

import xlrd
year_b, month_b, day_b, hour_b, minute_b, second_b = xlrd.xldate_as_tuple(udtdate_ib,book.datemode)
as_udtdate_ib = str(year_b) + '/' + str(month_b) + '/' + str(day_b)

Recommended Posts

Get the update date of the Python memo file.
[Python] Get the update date of a news article from HTML
[Python] Get the last updated date of the website
[Python] Get the official file path of the shortcut file (.lnk)
Get the site update date seriously
Check the existence of the file with python
[Python3] Understand the basics of file operations
Get the contents of git diff from python
[Python] Get / edit the scale label of the figure
[Python] Get the main topics of Yahoo News
Get the caller of a function in Python
the zen of Python
Check the date of the flag duty with Python
[Python3] Get date diff
Get date in Python
[Python] Split the date
Memo of the program to get the date in two digits with javascript, Ruby, Python, shell script
At the time of python update on ubuntu
Convert the character code of the file with Python3
Get the size of the image file on the web (Python3, no additional library required)
Template of python script to read the contents of the file
How to get the number of digits in Python
[Python] Get the text of the law from the e-GOV Law API
[python] Get the list of classes defined in the module
Get the return code of the Python script from bat
The pain of gRPC using Python. November 2019. (Personal memo)
Get the size (number of elements) of UnionFind in Python
[Python] Get the list of ExifTags names of Pillow library
Get the operation status of JR West with Python
Script to get the expiration date of the SSL certificate
[Python] Get the number of views of all posted articles
Get the URL of the HTTP redirect destination in Python
Towards the retirement of Python2
Summary of python file operations
Download the file in Python
Automatic update of Python module
About the ease of Python
Get the number of digits
The Power of Pandas: Python
Try to get the function list of Python> os package
[Memo] The mystery of cumulative assignment statements in Python functions
Get the MIME type in Python and determine the file format
Get the number of specific elements in a python list
[Note] Import of a file in the parent directory in Python
Google search for the last line of the file in Python
Extract the xz file with python
The story of Python and the story of NaN
[Python] Operation memo of pandas DataFrame
Easy encryption of file contents (Python)
Get the number of views of Qiita
First Python 3 ~ The beginning of repetition ~
Save the binary file in Python
Get the desktop path in Python
Existence from the viewpoint of Python
Get the weather with Python requests
Get the weather with Python requests 2
pyenv-change the python version of virtualenv
Get the script path in Python
How to get the Python version
Get the attributes of an object
Change the Python version of Homebrew