#导入的方法是: import os |
import os s = os.getcwd() # s 中保存的是当前目录(即文件夹) |
import os import time folder = time.strftime(r"%Y-%m-%d_%H-%M-%S",time.localtime()) os.makedirs(r'%s/%s'%(os.getcwd(),folder)) |
import os os.chdir( "C:\\123") |
import os #fpath , fname = os.path.split( "你要分解的路径") #例如: a, b = os.path.split( "c:\\123\\456\\test.txt" ) print a print b >>> c:\123\456 # 输出 >>> test.txt # 输出 |
import os fpathandname , fext = os.path.splitext( "你要分解的路径") #例如: a, b = os.path.splitext( "c:\\123\\456\\test.txt" ) print a print b #输出 >>> c:\123\456\test >>> .txt |
b = os.path.exists( "你要判断的路径") #返回值b: True 或 False |
b = os.path.isfile( "你要判断的路径") #返回值b: True 或 False |
b = os.path.isdir( "你要判断的路径") #返回值b: True 或 False |
L = os.listdir( "你要判断的路径") #例如: L = os.listdir( "c:/" ) print L #输出 >>> ['1.avi', '1.jpg', '1.txt', 'CONFIG.SYS', 'Inetpub', 'IO.SYS', 'KCBJGDJC', 'KCBJGDYB', 'KF_GSSY_JC', 'MSDOS.SYS', 'MSOCache', 'NTDETECT.COM', 'ntldr', 'pagefile.sys', 'PDOXUSRS.NET', 'Program Files', 'Python24', 'Python31', 'QQVideo.Cache', 'RECYCLER', 'System Volume Information', 'TDDOWNLOAD', 'test.txt', 'WINDOWS'] |
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:\\" ) |
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:\\" ) |
os.makedirs( path ) # path 是"要创建的子目录" #例如: os.makedirs( "C:\\123\\456\\789") |
os.rmdir( path ) # path: "要删除的子目录" |
os.remove( filename ) # filename: "要删除的文件名" |
import os os.name( oldfileName, newFilename) |
欢迎光临 openKylin论坛 (https://forum.openkylin.top/) | Powered by Discuz! X3.3 |