os+sys.path+sys.argv
os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") 改变当前脚本工作目录;相当于shell下cd os.curdir 返回当前目录: ('.') os.pardir 获取当前目录的父目录字符串名:('..') os.mkdir('dirname') 生成单级目录;相当于shell中mkdir dirname os.makedirs('dirname1/dirname2') 可生成多层递归目录 os.rmdir('dirname') 删除单级空目录,若目录不为空则无法删除,报错;相当于shell中rmdir dirname os.removedirs('dirname1') 若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推 os.remove() 删除一个文件 os.rename("oldname","newname") 重命名文件/目录 os.listdir('dirname') 列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印 os.stat('path/filename') 获取文件/目录信息 os.symlink('path/filename','ln_filename') 创建符号链接,源需绝对路径 os.utime() 修改时间属性
>>> stinfo = os.stat('c.py') >>> print "access time of c.py: %s \nmodified time of c.py: %s" % (stinfo.st_atime,stinfo.st_mtime) access time of c.py: 1375448908.0 modified time of c.py: 1369735909.0 >>> os.utime('c.py',(1375448978,1369735977)) >>> print "access time of c.py: %s \nmodified time of c.py: %s" % (stinfo.st_atime,stinfo.st_mtime) access time of c.py: 1375448908.0 modified time of c.py: 1369735909.0 退出Python交互模式,再次进入 >>> import os >>> stinfo = os.stat('c.py') >>> print "access time of c.py: %s \nmodified time of c.py: %s" % (stinfo.st_atime,stinfo.st_mtime) access time of c.py: 1375448978.0 modified time of c.py: 1369735977.0
os.walk() 生成一个目录树下的所有文件名 os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]]) top表示需要遍历的目录树的路径 topdown的默认值是”True”,表示首先返回目录树下的文件,然后在遍历目录树的子目录.Topdown的值为”False”时,则表示先遍历目录树的子目录,返回子目录下的文件,最后返回根目录下的文件 onerror的默认值是”None”,表示忽略文件遍历时产生的错误.如果不为空,则提供一个自定义函数提示错误信息后继续遍历或抛出异常中止遍历 该函数返回一个元组,该元组有3个元素,这3个元素分别表示每次遍历的路径名,目录列表和文件列表 os.walk()举例: >>> import os >>> for root, dirs, files in os.walk("wd/chat", topdown=False): ... for name in files: ... print(os.path.join(root, name)) #打印文件绝对路径 ... for name in dirs: ... print(os.path.join(root, name)) #打印目录绝对路径
...
os.tmpfile() 创建并打开‘w+b'一个新的临时文件
os.sep 输出操作系统特定的路径分隔符,win下为"\\",Linux下为"/"
os.linesep 输出当前平台使用的行终止符,win下为"\t\n",Linux下为"\n"
os.pathsep 输出用于分割文件路径的字符串
os.name 输出字符串指示当前使用平台。win->'nt'; Linux->'posix'
os.system("bash command") 运行shell命令,直接显示
os.popen("bash command") 运行shell命令,生成对象,可赋给变量,再用read读取 >>> import os >>> os.system('ls twisted') chat_client_twisted.py chat_server_twisted.py 0 >>> LS = os.popen('ls twisted') >>> LS.readlines() ['chat_client_twisted.py\n', 'chat_server_twisted.py\n']
os.environ 获取系统环境变量
os.access('pathfile',os.W_OK) 检验文件权限模式,输出True,False
os.chmod('pathfile',os.W_OK) 改变文件权限模式
# echo 'test' > test.sh >>> os.access('test.sh',os.W_OK) True >>> os.access('test.sh',os.X_OK) False >>> os.chmod('test.sh',os.X_OK) >>> os.access('test.sh',os.X_OK) True # ls -l test.sh ---------x 1 root root 12 Oct 20 23:03 test.sh
os.path常用模块详解
os.path.abspath(path) 返回path规范化的绝对路径 >>> import os.path >>> os.path.abspath('c.py') '/root/py/c.py' >>> os.path.abspath('../py/c.py') '/root/py/c.py'
os.path.split(path) 将path分割成目录和文件名二元组返回 >>> os.path.split('/root/py/c.py') ('/root/py', 'c.py') >>> os.path.split('/root/py/') ('/root/py', '')
os.path.dirname(path) 返回path的目录。其实就是os.path.split(path)的第一个元素 >>> os.path.dirname('/root/py/c.py') '/root/py' >>> os.path.dirname('c.py') ''
os.path.basename(path) 返回path最后的文件名。如何path以/或\结尾,那么就会返回空值。即os.path.split(path)的第二个元素 >>> os.path.basename('/root/py/c.py') 'c.py' >>> os.path.basename('/root/py') 'py'
os.path.commonprefix(list) 返回list中,所有path共有的最长的路径,从左向右,相同字符
os.path.exists(path) 如果path存在,返回True;如果path不存在,返回False
os.path.isabs(path) 如果path是绝对路径,返回True
os.path.isfile(path) 如果path是一个存在的文件,返回True。否则返回False
os.path.isdir(path) 如果path是一个存在的目录,则返回True。否则返回False
os.path.join(path1[, path2[, ...]]) 将多个路径组合后返回,第一个绝对路径之前的参数将被忽略
os.path.normcase(path) 在Linux下,该函数会原样返回path,在windows平台上会将路径中所有字符转换为小写,并将所有斜杠转换为反斜杠 >>> os.path.normcase('c:/windows\\system32\\') 'c:\\windows\\system32\\'
os.path.normpath(path) 规范化路径 >>> os.path.normpath('c://windows\\System32\\../Temp/') 'c:\\windows\\Temp'
os.path.splitdrive(path) 拆分驱动器名和路径,主要对win,对linux元组第一个总是空的 >>> os.path.splitdrive('c:\\windows') ('c:', '\\windows')
os.path.splitext(path) 分离文件名与扩展名;默认返回(fname,fextension)元组,可做分片操作 ,以“.”为分隔符 >>> os.path.splitext('/root/py/c.py') ('/root/py/c', '.py')
os.path.getsize(path) 返回path的大小(字节)
os.path.getatime(path) 返回path所指向的文件或者目录的最后存取时间
os.path.getmtime(path) 返回path所指向的文件或者目录的最后修改时间
os.path.walk(top,func,arg)
- top表示需要遍历的目录树的路径
- func表示回调函数,对遍历路径进行处理.所谓回调函数,是作为某个函数的参数使用,当某个时间触发时,程序将调用定义好的回调函数处理某个任务.回调函数必须提供3个参数:第1个参数为walk()的参数tag,第2个参数表示目录列表,第3个参数表示文件列表
- arg是传递给回调参数func的元组.回调函数的一个参数必须是arg,为回调函数提供处理参数.参数arg可以为空
>>> import os >>> def VisitDir(arg,dirname,names): ... for filespath in names: ... print os.path.join(dirname,filespath) ... >>> path='/root/py/wd/chat' >>> os.path.walk(path,VisitDir,()) /root/py/wd/chat/chat_server.py /root/py/wd/chat/chat_client.py /root/py/wd/chat/test /root/py/wd/chat/test/linuxeye /root/py/wd/chat/test/test2 /root/py/wd/chat/test/test3 /root/py/wd/chat/test/test2/asdf /root/py/wd/chat/test/test3/sdfaxx
os.path.walk()与os.walk()产生的文件名列表并不相同。os.path.walk()产生目录树下的目录路径和文件路径,而os.walk()只产生文件路径
如何将路径“永久"添加到sys.path?
sys.path是python的搜索模块的路径集,是一个list
可以在python 环境下使用sys.path.append(path)添加相关的路径,但在退出python环境后自己添加的路径就会自动消失!
在python脚本中修改
sys.path.append('c:\\mypythonlib')
为解决这个问题,可以有以下方法:
1. 将自己做的py文件放到 site_packages 目录下:
下面命令显示了 site-packages 目录:
但是这样做会导致一个问题,即各类模块都放到此文件夹的话,会导致乱的问题,这一点是显而易见的。
注意,也不创建子文件夹,再将自己的模块放到子文件夹解决问题,这会导致使用import 语句时错误。
2. 使用pth文件,在 site-packages 文件中创建 .pth文件,将模块的路径写进去,一行一个路径,以下是一个示例,pth文件也可以使用注释:
# .pth file for the my project(这行是注释)
E:\DjangoWord
E:\DjangoWord\mysite
E:\DjangoWord\mysite\polls
这个不失为一个好的方法,但存在管理上的问题,而且不能在不同的python版本中共享。
3. 使用PYTHONPATH环境变量,在这个环境变量中输入相关的路径,不同的路径之间用逗号(英文的!)分开,如果PYTHONPATH 变量还不存在,可以创建它!
路径会自动加入到sys.path中,而且可以在不同的python版本中共享,应该是一样较为方便的方法。
关于与python相关的环境变量有那些,请参考:
http://docs.python.org/using/cmdline.html
在页面上找到PYTHONPATH
sys.argv[]是用来获取命令行参数的,sys.argv[0]表示代码本身文件路径,所以参数从1开始,以下两个例子说明:
1、使用sys.argv[]的一简单实例:
以下是sample1.py文件:
import sys,os print sys.argv os.system(sys.argv[1])
这个例子os.system接收命令行参数,运行参数指令,cmd命令行带参数运行python sample1.py notepad,将打开记事本程序。
2、这个例子是简明python教程上的,明白它之后你就明白sys.argv[]了。
以下是sample.py文件:
#!/usr/bin/env python #_*_ coding:utf-8 _*_ import sys def readfile(filename): #定义readfile函数,从文件中读出文件内容 '''''''Print a file to the standard output.''' f = file(filename) while True: line = f.readline() if len(line) == 0: break print line, # notice comma 分别输出每行内容 f.close() # Script starts from here print sys.argv if len(sys.argv) < 2: print 'No action specified.' sys.exit() if sys.argv[1].startswith('--'): option = sys.argv[1][2:] # fetch sys.argv[1] but without the first two characters if option == 'version': #当命令行参数为-- version,显示版本号 print 'Version 1.2' elif option == 'help': #当命令行参数为--help时,显示相关帮助内容 print ''' This program prints files to the standard output. Any number of files can be specified. Options include: --version : Prints the version number --help : Display this help''' else: print 'Unknown option.' sys.exit() else: for filename in sys.argv[1:]: #当参数为文件名时,传入readfile,读出其内容 readfile(filename)
在与sample.py同一目录下,新建3个记事本文件test.txt,test1.txt,test2.txt,内容如下图:
验证sample.py,如下:
C:\Users\91135\Desktop>python sample.py
['sample.py']
No action specified.
C:\Users\91135\Desktop>python sample.py --help
['sample.py', '--help']
This program prints files to the standard output.
Any number of files can be specified.
Options include:
--version : Prints the version number
--help : Display this help
C:\Users\91135\Desktop>python sample.py --version
['sample.py', '--version']
Version 1.2
C:\Users\91135\Desktop>python sample.py --ok
['sample.py', '--ok']
Unknown option.
C:\Users\91135\Desktop>python sample.py test.txt
['sample.py', 'test.txt']
hello python!
C:\Users\91135\Desktop>python sample.py test.txt test1.txt test2.txt
['sample.py', 'test.txt', 'test1.txt', 'test2.txt']
hello python!
hello world!
hello wahaha!
goodbye!
C:\Users\91135\Desktop>
posted on 2017-08-15 19:25 myworldworld 阅读(200) 评论(0) 收藏 举报