打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
python对文件 文件夹的操作

http://elan1986.iteye.com/blog/1123579

2011
  1. #-*-coding: UTF-8 -*-  
  2.   
  3. import os  
  4.   
  5.   
  6. s = os.getcwd()#获取当前目录  
  7. print s  
  8.   
  9. os.chdir("E:\\PyWk\\nodepad_py") #更改当前目录  
  10.   
  11. fpath, fname = os.path.split("E:\\PyWk\\nodepad_py\\09.py") #将一个路径分解为目录名和文件名  
  12. print fpath, fname  
  13.   
  14. fpathandname, fext = os.path.splitext("E:\\PyWk\\nodepad_py\\09.py") #分解文件名的扩展名  
  15. print fpathandname, fext  
  16.   
  17. a = os.path.exists("E:\\PyWk\\nodepad_py\\erro.py") #判断文件或目录是否存在  
  18. print a  
  19.   
  20. b = os.path.isfile("E:\\PyWk\\nodepad_py\\09.py") #判断是否文件  
  21. print b  
  22.   
  23. c = os.path.isdir("E:\\PyWk\\nodepad_py\\09.py") #判断是否是目录  
  24. print c  
  25.   
  26. list = os.listdir("E:\\PyWk\\nodepad_py") #获取目录下的文件以及子目录列表  
  27. print list  
  28.   
  29. #os.makedirs("c:\\test1\\test2\\test3") #创建子目录  
  30.   
  31. #f = open("c:\\test1\\test2\\test3\\test4.txt", 'w') #创建一个空文件  
  32. #f.close()  
  33.   
  34. #os.rmdir("c:\\test1\\test2\\test3") #删除子目录  
  35.   
  36. #os.remove("c:\\test1\\test2\\test3\\test4.txt") #删除文件  
  37.   
  38.   
  39. os.chdir('c:\\test1\\test2\\test3')  
  40. print os.getcwd()  
  41. list = os.listdir(os.getcwd())  
  42. print list  
  43. os.rename("test4.txt", "newtest4.txt")  


Python代码  
  1. #-*-coding: UTF-8 -*-  
  2. #显示某一目录下所有文件夹  
  3. import os  
  4. def getDirList(p):  
  5.     p = str(p)  
  6.     if p == '':  
  7.         return []  
  8.     p = p.replace('\\', '\\\\')  
  9.     if p[-1] != "\\":  
  10.         p = p+"\\"  
  11.     a = os.listdir(p)  
  12.     b = [x for x in a if os.path.isdir(p+x)]  
  13.     return b  
  14. print getDirList("c:\\")  



Python代码  
  1. #-*-coding: UTF-8 -*-  
  2. #获取某目录下所有文件列表  
  3. import os  
  4. def getFileList(p):  
  5.     p = str(p)  
  6.     if p == "":  
  7.         return []  
  8.     p = p.replace('\\', '\\\\')  
  9.     if p[-1] != '\\':  
  10.         p = p + '\\'  
  11.     a = os.listdir(p)  
  12.     b = [x for x in a if os.path.isfile(p+x)]  
  13.     return b  
  14.   
  15. print getFileList("c:\\")  
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Python生成pyd文件
用Python遍历目录
python中文件目录操作的常见方法
Python中的文件夹与文件路径
python
python的目录操作
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服