Skip to content

Instantly share code, notes, and snippets.

@litefeel
Last active October 22, 2015 07:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save litefeel/5ba498293cd166895595 to your computer and use it in GitHub Desktop.
Save litefeel/5ba498293cd166895595 to your computer and use it in GitHub Desktop.
python 脚本
#!/usr/bin/python
# coding=utf-8
import os.path
import sys
import shutil
"""
getpath.py [path]
命令行所在目录
或 命令行所在目录与path的组合
"""
def get_script_path():
# __file__ 当前脚本完整文件名
return os.path.dirname(__file__)
def getpath():
dir = '.'
if len(sys.argv) == 2:
dir = sys.argv[1]
return os.path.abspath(dir)
def readfile(filename):
if not os.path.exists(filename):
return None
with open(filename) as f:
data = f.read()
f.close()
return data
def writefile(filename, data):
with open(filename, 'w') as f:
f.write(data)
f.close()
def appendfile(filename, data):
with open(filename, 'a') as f:
f.write(data)
f.write('\n')
f.close()
def md5file(filename):
with open(filename, 'rb') as f:
data = f.read()
f.close()
m = hashlib.md5()
m.update(data)
return m.hexdigest()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment