Operation of directories in 6.python

Source: Internet
Author: User

Put together the code, and gradually add:
  
 
  1.   Import OS
  2.     #1.os.getcwd()         #获取当前目录
  3. print os.getcwd()         #示例
  4. print os.getcwd()+‘\\resources\\baidu.txt‘         #获取当前目录下的resources文件夹下的baidu.txt文件
  5. print ‘----当前目录.形式:‘,os.curdir        #返回当前目录(‘.‘)
  6. #2.os.listdir(dirname)      #列出dirname下的目录和文件
  7. print ‘----当前目录下所有文件:‘,os.listdir(os.getcwd())#相当于在os.getcwd()目录下执行dir命令,返回为list类型
  8. #3.os.chdir(dirname) #改变工作目录到dirname
  9. os.chdir(‘d:\\workspace‘) #示例 改变工作目录到d盘下的workspace目录
  10. print ‘----改变目录后重新打印目录:‘,os.getcwd()
  11. #4.os.path.abspath(name) #获得当前所在绝对路径 并追加name值
  12. print ‘----当前绝对路径加name值‘,os.path.abspath(‘a\\b\\c‘)#示例
  13. #5.os.path.join(path,name) #连接目录与文件名或目录
  14. print ‘----连接目录与文件名或目录‘,os.path.join(os.getcwd(),‘test‘) #示例 当前目录连接test文件名称
  15. #6.os.path.dirname(path) #返回文件路径
  16. print ‘----返回文件路径‘,os.path.dirname(os.getcwd()) #示例
  17. #7os.path.basename(path) #返回文件名
  18. print ‘----返回文件名‘,os.path.basename(os.getcwd()) #示例
  19. #os.remove(dir) #dir为要删除的文件夹或者文件路径
  20. #os.rmdir(path) #path要删除的目录的路径。需要说明的是,使用os.rmdir删除的目录必须为空目录,否则函数出错
  21. #os.path.isdir(name) #判断name是不是一个目录,name不是目录就返回false
  22. #os.path.isfile(name) #判断name是不是一个文件,不存在name也返回false
  23. #os.path.exists(name) #判断是否存在文件或目录name
  24. #os.path.getsize(name) #获得文件大小,如果name是目录返回0L



From for notes (Wiz)

Operation of directories in 6.python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.