http://elan1986./blog/11235792011 - #-*-coding: UTF-8 -*-
-
- import os
-
-
- s = os.getcwd()#获取当前目录
- print s
-
- os.chdir("E:\\PyWk\\nodepad_py") #更改当前目录
-
- fpath, fname = os.path.split("E:\\PyWk\\nodepad_py\\09.py") #将一个路径分解为目录名和文件名
- print fpath, fname
-
- fpathandname, fext = os.path.splitext("E:\\PyWk\\nodepad_py\\09.py") #分解文件名的扩展名
- print fpathandname, fext
-
- a = os.path.exists("E:\\PyWk\\nodepad_py\\erro.py") #判断文件或目录是否存在
- print a
-
- b = os.path.isfile("E:\\PyWk\\nodepad_py\\09.py") #判断是否文件
- print b
-
- c = os.path.isdir("E:\\PyWk\\nodepad_py\\09.py") #判断是否是目录
- print c
-
- list = os.listdir("E:\\PyWk\\nodepad_py") #获取目录下的文件以及子目录列表
- print list
-
- #os.makedirs("c:\\test1\\test2\\test3") #创建子目录
-
- #f = open("c:\\test1\\test2\\test3\\test4.txt", 'w') #创建一个空文件
- #f.close()
-
- #os.rmdir("c:\\test1\\test2\\test3") #删除子目录
-
- #os.remove("c:\\test1\\test2\\test3\\test4.txt") #删除文件
-
-
- os.chdir('c:\\test1\\test2\\test3')
- print os.getcwd()
- list = os.listdir(os.getcwd())
- print list
- os.rename("test4.txt", "newtest4.txt")
- #-*-coding: UTF-8 -*-
- #显示某一目录下所有文件夹
- import os
- def getDirList(p):
- p = str(p)
- if p == '':
- return []
- p = p.replace('\\', '\\\\')
- if p[-1] != "\\":
- p = p+"\\"
- a = os.listdir(p)
- b = [x for x in a if os.path.isdir(p+x)]
- return b
- print getDirList("c:\\")
- #-*-coding: UTF-8 -*-
- #获取某目录下所有文件列表
- import os
- def getFileList(p):
- p = str(p)
- if p == "":
- return []
- p = p.replace('\\', '\\\\')
- if p[-1] != '\\':
- p = p + '\\'
- a = os.listdir(p)
- b = [x for x in a if os.path.isfile(p+x)]
- return b
-
- print getFileList("c:\\")
|