Senin, 09 Desember 2013

Convert time.struct_time to datetime in Python

>>> structTime = time.localtime()
>>> datetime.datetime(*structTime[:6])
datetime.datetime(2009, 11, 8, 20, 32, 35)

Tips for Python


  1. Use [Ctrl+Alt+Enter] to configure interactive console in Eclipse-PyDev (Console for currently active editor)
  2. Use print('x',...) to print without newline concate with list
    print('x', os.listdir(self.right))
    
  3. Enable PyDev Interactive Console while Debugging Session
    To enable, go to Window | Preferences | PyDev | Interactive Console and check 'Connect console to Debug Session?
  4. If you get an error message like this
    Assignment to reserved built-in symbos: abs
    , then then you can add comments
    # @ReservedAssignment
     at the end of the line so that it is no longer regarded as an error
  5. ...

Get Modification Time of File in Python

'''
Created on Dec 9, 2013

@author: BalonCoding
'''
import os
  
fileName = 'c:\\fm\\fmb\\MTR00035.fmb'
#using os.stat.st_mtime
st=os.stat(fileName)    
mtime=st.st_mtime
print("mtime (using os.stat.st_mtime) = " + str(mtime))
#mtime (using os.stat.st_mtime) = 1141933677.28125

#using os.path.getmtime
print("mtime (using os.path.getmtime) = " + str(os.path.getmtime(fileName)))
#mtime (using os.path.getmtime) = 1141933677.28125


############################################################################################
import datetime

#using datetime.datetime
print("mtime (using datetime.datetime) = {}".format(datetime.datetime.fromtimestamp(mtime)))
#mtime (using datetime.datetime) = 2006-03-10 02:47:57.281250


############################################################################################
import stat, time
print("mtime (using time.ctime ) = {}".format(time.ctime(st[stat.ST_MTIME])))
#mtime (using time.ctime ) = Fri Mar 10 02:47:57 2006