• python os与os.path模块


    os

    os.chdir('/temp')    改变当前工作目录

    os.getcwd()    返回当前工作目录

    os.mkdir('example')    创建目录

    os.listdir('cwd')    列出指定的文件夹包含的文件或文件夹的名字的列表

    os.rename('test', 'filetest.txt')    重命名文件[或目录]

    os.remove('d:aa.txt')    删除文件

    os.pardir    返回的是上级目录

    os.rmdir(path)  删除path指定的空目录,如果目录非空,则抛出一个OSError异常

    os.removedirs(path)  递归删除目录

    os.path

    os.path.isdir('/temp')    指定路径是否存在且是否为一个目录

    os.path.join('cwd', 'os.listdir(cwd)[0]')    将路径名和文件名组合成完整路径

    os.path.split(path)    返回(dirname(), basename())元组,即将path分割成目录和文件名二元组返回

    os.path.splitext(os.path.basename(path))    返回(filename, extension)元组;返回文件名,文件扩展名元组

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

    os.remove(path)   删除路径为path的文件

     以下代码摘自《Python核心编程第二版》:

     1 #!/usr/bin/env python
     2 # -*- coding:utf-8 -*-
     3 
     4 import os
     5 
     6 
     7 for tmpdir in ('/tmp', r'D:Python_filePY'):
     8     if os.path.isdir(tmpdir):#指定路径是否存在且是否为一个目录
     9         break
    10     else:
    11         print 'no temp directory available'
    12         tmpdir = ''
    13 
    14 if tmpdir:
    15     os.chdir(tmpdir)#改变当前工作目录
    16     cwd = os.getcwd()
    17     print '*** current temporary directory'
    18     print cwd
    19 
    20     print '*** creating example directory: '
    21     print cwd
    22     print '*** original directory listing: '
    23     print os.listdir(cwd) #返回(path)指定的文件夹包含的文件或文件夹的名字的列表
    24     print '*** creating test file ...'
    25     fobj = open('test', 'w')
    26     fobj.write('foo
    ')
    27     fobj.write('bar
    ')
    28     fobj.close()
    29     print '*** updated directory listing: '
    30     print os.listdir(cwd)
    31 
    32     print "*** renaming 'test' to 'filetest.txt'"
    33     os.rename('test', 'filetest.txt') #重命名文件[或目录]
    34     print '*** updated directory listing: '
    35     print os.listdir(cwd)
    36 
    37     path = os.path.join(cwd, os.listdir(cwd)[1])
    38     print '*** full file pathname'
    39     print path
    40     print '*** (pathname, basename) =='
    41     print os.path.split(path) #将path分割成目录和文件名二元组返回。
    42     print '*** (filename, extension) =='
    43     print os.path.splitext(os.path.basename(path)) #os.path.basename(path)返回文件名;os.path.splitext返回文件名,文件扩展名元组
    44 
    45     print '*** displaying file contents: '
    46     fobj = open(path)
    47     for eachLine in fobj:
    48         print eachLine,
    49     fobj.close()
    50 
    51     print '*** deleting test file'
    52     os.remove(path) #删除路径为path的文件
    53     print '*** updated directory listing: '
    54     print os.listdir(cwd)
    55     os.chdir(os.pardir) #os.pardir获取当前目录的父目录字符串名
    56     print '*** deleting test directory'
    57     os.rmdir('example')
    58     print '*** DONE'
     1 #!/usr/bin/env python
     2 # -*- coding:utf-8 -*-
     3 
     4 import os
     5 
     6 print os.path.isdir('D:\Python_file')
     7 os.chdir('D:\Python_file')
     8 cwd = os.getcwd()
     9 print cwd
    10 
    11 os.mkdir('example')
    12 os.chdir('example')
    13 cwd = os.getcwd()
    14 print cwd
    15 print os.listdir(cwd)
    16 
    17 fobj = open('test', 'w')
    18 fobj.write('foo
    ')
    19 fobj.write('bar
    ')
    20 fobj.close()
    21 print os.listdir(cwd)
    22 
    23 os.rename('test', 'filetest.txt')
    24 print os.listdir(cwd)
    25 
    26 path = os.path.join(cwd, os.listdir(cwd)[0])
    27 print path
    28 
    29 print os.path.isfile(path)
    30 print os.path.isdir(path)
    31 
    32 print os.path.split(path)
    33 print os.path.splitext(os.path.basename(path))
    34 
    35 fobj = open(path)
    36 for eachLine in fobj:
    37     print eachLine,
    38 
    39 fobj.close()
    40 os.remove(path)
    41 print os.listdir(cwd)
    42 os.chdir(os.pardir)
    43 os.rmdir('example')
  • 相关阅读:
    五种方法来遍历Map
    怎样去理解Java中的volatile
    大二层网络----Vxlan技术
    HTTP请求响应过程
    TCP数据传输
    TCP标志位
    TCP协议中的三次握手和四次挥手(图解)
    HTTP报文分析
    HTTP报文图示
    DNS数据包结构
  • 原文地址:https://www.cnblogs.com/xyu1/p/7956443.html
Copyright © 2020-2023  润新知