Python interview - sys & os

工作中碰到需要很多的VM,以及不同user对应不同的python环境。debug的时候碰到问题:这个包明明在可见的路径下,并且有__init__.py,为什么python看不到(不能导入)。


用Python的都应该很熟悉的,能够导入的情况有大概三种:

1. .py 文件在当前目录下

2. .py 文件的path被添加到了sys.path的list里面

3. .py 文件所在的package里面有__init__.py  文件,所以python可以识别



对应的,先简单的对1 和3 进行的尝试,还是不能导入module。接着在命令行对sys.path进行尝试,明明是append了需要的模块的路径,但确实是找不到module。

最后发现是因为使用了VM,然后安装plugin的时候没有考虑user的权限问题,最后用chown授权了之后整个流程就能自动化跑起来了。


===========以上是一个简单的日常case,接着就是整理sys & os 模块 ============


sys 模块


import sys

print sys.argv # 程序本身的绝对路径
# ['/Users/PycharmProjects/CSDN_test/modules/test_sys.py']

print sys.modules # 返回所有已经导入的module列表. key - value 字典模式. key是模块名, value是模块.
# {'copy_reg': <module 'copy_reg' from '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/copy_reg.pyc'>, 'site': <module 'site' from '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site.pyc'>, '__builtin__': <module '__builtin__' (built-in)>, '__main__': <module '__main__' from '/Users/PycharmProjects/CSDN_test/modules/test_sys.py'>, 'abc': <module 'abc' from '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/abc.pyc'>, 'posixpath': <module 'posixpath' from '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/posixpath.pyc'>, 'errno': <module 'errno' (built-in)>, '_abcoll': <module '_abcoll' from '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/_abcoll.pyc'>, 'types': <module 'types' from '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/types.pyc'>, '_warnings': <module '_warnings' (built-in)>, 'genericpath': <module 'genericpath' from '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/genericpath.pyc'>, 'stat': <module 'stat' from '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/stat.pyc'>, 'zipimport': <module 'zipimport' (built-in)>, 'warnings': <module 'warnings' from '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/warnings.pyc'>, 'UserDict': <module 'UserDict' from '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/UserDict.pyc'>, 'sys': <module 'sys' (built-in)>, 'os.path': <module 'posixpath' from '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/posixpath.pyc'>, 'signal': <module 'signal' (built-in)>, 'linecache': <module 'linecache' from '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/linecache.pyc'>, 'posix': <module 'posix' (built-in)>, 'exceptions': <module 'exceptions' (built-in)>, 'os': <module 'os' from '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/os.pyc'>}

print sys.exc_info() # 获取当前正在处理的异常的 (type, value, traceback) 当前处理的异常的详细信息.
# (None, None, None)

print sys.hexversion # 获取python解释程序的版本值, 16进制格式.
# 33950192

print sys.version # 获取python的版本信息
# 2.6.9 (unknown, Aug 22 2015, 20:33:41)
# [GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.1)]

print sys.maxint # 最大Int值
# 9223372036854775807

print sys.maxunicode # 最大Unicode值
# 65535

print sys.path # 返回模块的搜索路径, 初始化时候使用的PYTHONPATH环境变量, 以及安装模块时default的路径
# ['/Users/PycharmProjects/CSDN_test/modules', '/Users/PycharmProjects/CSDN_test', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python26.zip', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-dynload', '/Library/Python/2.6/site-packages', '/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/PyObjC']


print sys.stdout # this is the file handle that your process reads to get information from you. 标准输入.
# <open file '<stdout>', mode 'w' at 0x11012c150>

print sys.stdin # your process writes normal information to this file handle. 标准输出.
# <open file '<stdin>', mode 'r' at 0x11012c0c0>

print sys.stderr # your process writes error information here. 程序错误输出.
# <open file '<stderr>', mode 'w' at 0x11012c1e0>

print sys.api_version # 解释器的C的API版本
# 1013

print sys.version_info # A tuple containing the five components of the version number: major, minor, micro, releaselevel, and serial.
# (2, 6, 9, 'final', 0)

print sys.executable # python 解释程序路径
# /System/Library/Frameworks/Python.framework/Versions/2.6/bin/python2.6

print sys.stdout.write("123") # 屏幕输出
# 123None

print sys.getfilesystemencoding() # 获取文件系统使用编码方式,Windows下返回'mbcs',mac下返回'utf-8'.
# utf-8

print sys.getdefaultencoding() # 获取系统当前编码,一般默认为ascii。
# ascii

 os 模块

import os

print os.name # 输出字符串指示正在使用的平台. 如果是windows则用'nt'表示,对于Linux/Unix用户,则是'posix'
# posix

os.mkdir(path [,mode=0777]) # 创建目录

os.rmdir() # 删除目录

os.mkdirs() # 创建多层目录

os.removedirs() # 删除多层目录,每层都是空的,除了要删除的目录外

print os.getcwd() # 函数得到当前工作目录,即当前python脚本工作的目录路径
# /Users/PycharmProjects/CSDN_test/modules

os.listdir() # 返回指定目录下得所有文件和目录名

print os.listdir(os.getcwd()) # 当前目录下得所有文件和目录名
# ['__init__.py', 'test_os.py', 'test_sys.py']

os.remove() # 删除一个文件

os.system() # 运行shell命令

os.sep # 可以取代操作系统特定的路径分隔符

print os.linesep # 给出当前平台使用的行终止符

os.path.split() # 返回一个路径的目录名和文件名

os.path.isfile() # 检验路径是否是文件,返回True或者False

os.path.isdir() # 检验路径是否是目录,返回True或者False

os.path.exists() # 检验路径是否存在,返回True或者False

os.path.abspath(name) # 获得绝对路径

os.path.normpath(path) # 规范path的字符串格式

os.path.getsize(name) # 获得文件大小,如果name是一个目录,返回OL

os.path.splitext() # 分离文件名与扩展名

os.path.join(path,name) # 连接目录与文件名或者目录

os.path.basename(path) # 返回文件名 

os.path.dirname(path) # 返回文件路径





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值