>>> structTime = time.localtime() >>> datetime.datetime(*structTime[:6]) datetime.datetime(2009, 11, 8, 20, 32, 35)
Senin, 09 Desember 2013
Convert time.struct_time to datetime in Python
Tips for Python
- Use [Ctrl+Alt+Enter] to configure interactive console in Eclipse-PyDev (Console for currently active editor)
- Use print('x',...) to print without newline concate with list
print('x', os.listdir(self.right)) - Enable PyDev Interactive Console while Debugging Session
To enable, go to Window | Preferences | PyDev | Interactive Console and check 'Connect console to Debug Session? - 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 - ...
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
Jumat, 29 November 2013
Disconnect and Map Network Drive on Windows with Python
I'm using Python version 3.3.2
- method using subprocess.Popen
- method using subprocess.call
# Balon Coding, 29 November 2013
# Map network drive using subprocess.Popen
# Drive letter: Q
# Shared drive path: \\192.168.51.1\balon$
# Username: balon
# Password: coding
import subprocess
# initialize
driveLetter = 'Q:'
networkPath = '\\\\192.168.51.1\balon$'
user = 'balon'
password = 'coding'
print('map network drive using subprocess.Popen')
# Disconnect anything on drive letter Q
winCMD = 'NET USE ' + driveLetter + ' /delete'
print(winCMD)
subprocess.Popen(winCMD, stdout=subprocess.PIPE, shell=True)
# Connect to map network drive to letter Q
winCMD = 'NET USE ' + driveLetter + ' ' + networkPath + ' /User:' + user + ' ' + password
print(winCMD)
subprocess.Popen(winCMD, stdout=subprocess.PIPE, shell=True)
# Balon Coding, 29 November 2013
# Map network drive using subprocess.call
# Drive letter: Q
# Shared drive path: \\192.168.51.1\balon$
# Username: balon
# Password: coding
import subprocess
# initialize
driveLetter = 'Q:'
networkPath = '\\\\192.168.51.1\balon$'
user = 'balon'
password = 'coding'
print('map network drive using subprocess.call')
# Disconnect anything on drive letter Q
winCMD = 'NET USE ' + driveLetter + ' /delete'
print(winCMD)
subprocess.call(winCMD, shell=True)
# Connect to map network drive to letter Q
winCMD = 'NET USE ' + driveLetter + ' ' + networkPath + ' /User:' + user + ' ' + password
print(winCMD)
subprocess.call(winCMD, shell=True)
Kamis, 28 November 2013
List Available Drive Letters In Python
I'm using Python version 3.3.2
import string
from ctypes import windll
def get_drives():
drives = []
bitmask = windll.kernel32.GetLogicalDrives()
for letter in string.ascii_uppercase:
if bitmask & 1:
drives.append(letter)
bitmask >>= 1
return drives
if __name__ == '__main__':
print (get_drives()) # On my PC, this prints ['C', 'D', 'W', 'X', 'Y', 'Z']
Minggu, 17 November 2013
Print 'Hello World' di Python
Pertama kali belajar di Python sudah menghadapi tantangan pertama, yakni hanya untuk memunculkan "Hello World".
Ternyata setelah gugel, ada yang menyatakan ini merupakan syntax yang berlaku di Python 2.x, sementara yang BalonCoding gunakan adalah Python 3.3 yang sudah menggunakan syntax print (). Sehingga untuk menjalankan print "Hello World", maka lakukan seperti berikut ini,
>>> print "Hello World!"
File "", line 1
print "Hello World!"
^
SyntaxError: invalid syntax
>>>
Ternyata setelah gugel, ada yang menyatakan ini merupakan syntax yang berlaku di Python 2.x, sementara yang BalonCoding gunakan adalah Python 3.3 yang sudah menggunakan syntax print (). Sehingga untuk menjalankan print "Hello World", maka lakukan seperti berikut ini,
text = "Hello World!" print (text)
Jumat, 01 November 2013
Starting emulator for AVD - Failed to allocate memory: 8
The solution is edit file config.ini [C:\Users\acer\.android\avd\.avd\
from
hw.ramSize=1024
to
hw.ramSize=1024MB
Langganan:
Postingan (Atom)