os.path.join('c' 'folder') # 'c\\folder'
os.path.join('c:' 'folder') # 'c:folder'
# Get file name.
os.path.basename('C:\\Windows\\123.txt') # '123.txt'
# Get file path and file extension.
filePath, fileExtension = os.path.splitext('C:\\Windows\\123.txt') # 'C:\\Windows\\123', '.txt'
# Get directory path.
os.path.dirname('C:\\Windows\\123.txt') # 'C:\\Windows'
# Get modify time from epoch time.
os.path.getmtime('C:') # 1409646173.7839031
# Traverse a directory.
for root, subdirs, files in os.walk("C:\\Pictures"):
print root, subdirs, files
2014年7月16日 星期三
[Python] 跟檔案有關的常用函式
2014年7月4日 星期五
[Python] How to get the last day of the month
想要知道某個特定月份有幾天,可以直接呼叫 calendar.monthrange(2014, 2),
會回傳第一天是禮拜幾和該月有幾天。
範例:
會回傳第一天是禮拜幾和該月有幾天。
範例:
import calendar calendar.monthrange(2014, 2) # (5, 28) # 禮拜六,28天 calendar.monthrange(2014, 3) # (5, 31) # 禮拜六,31天 calendar.monthrange(2014, 4) # (1, 30) # 禮拜二,30天
訂閱:
意見 (Atom)