收集的几个Python小技巧分享
获得当前机器的名字:
defhostname(): sys=os.name ifsys=='nt': hostname=os.getenv('computername') returnhostname elifsys=='posix': host=os.popen('echo$HOSTNAME') try: hostname=host.read() returnhostname finally: host.close() else: return'Unkwonhostname'
获取当前工作路径:
importos os.getcwd()
#or
#os.curdirjustreturn.forcurrentworkingdirectory. #needabspath()togetfullpath. os.path.abspath(os.curdir)