import osprint(dir(os))print(dir(os.path))
01、os.name()描述:顯示當前使用的平臺,'nt'表示Windows,'posix' 表示Linux
02、os.getcwd()os.getcwd()'C:\\Users\\wuzhengxiang'
03、os.chdir()#查看當前目錄os.getcwd()'C:\\Users\\wuzhengxiang'#重新設置當前工作空間os.chdir('C:/Users/wuzhengxiang/Desktop/股票數據分析')#再次查看當前目錄,已經變成新的了os.getcwd()'C:\\Users\\wuzhengxiang\\Desktop\\股票數據分析'
04、os.makedirs()描述:方法用於遞歸創建目錄。像 mkdir(), 但創建的所有intermediate-level文件夾需要包含子目錄。語法:os.makedirs(path, mode=0o777)os.makedirs('C:/Users/wuzhengxiang/Desktop/股票數據分析/1122', mode=0o777)
05、os.mkdir()描述:以數字權限模式創建目錄。默認的模式為 0777 (八進位)。語法:os.mkdir(path[, mode])os.mkdir('C:/Users/wuzhengxiang/Desktop/股票數據分析/2233', mode=0777 )
06、os.listdir()os.listdir('C:/Users/wuzhengxiang/Desktop/股票數據分析')['ETF研究.py', 'foo.txt', 'pi.txt', 'render.html']
os.listdir('.')['ETF研究.py', 'foo.txt', 'pi.txt', 'render.html']
07、os.remove()描述:用於刪除指定路徑的文件。如果指定的路徑是一個目錄,將拋出OSError。os.remove('C:/Users/zhengxiang.wzx/Desktop/timg.jpg')
08、os.rename()data_path = 'C:/Users/zhengxiang.wzx/Desktop/微博情緒識別'os.chdir(data_path)os.getcwd()'C:\\Users\\zhengxiang.wzx\\Desktop\\微博情緒識別'os.rename("圖片下載.py","圖片下載1.py")
09、os.renames()描述:用於遞歸重命名目錄或文件。類似rename()。既可以重命名文件, 也可以重命名文件的上級目錄名new --文件或目錄的新名字。甚至可以是包含在目錄中的文件,或者完整的目錄樹。os.chdir('C:/Users/wuzhengxiang/Desktop/Python知識點總結')os.getcwd()
os.renames("test/Python 63個內置函數詳解.py","test2/內置函數詳解.py")
os.listdir()['kaggle', 'test2', '股票分析', '課程資源'
10、os.linesep()描述:當前平臺用於分隔(或終止)行的字符串。它可以是單個字符,如 POSIX 上是 '\n',也可以是多個字符,如 Windows 上是 '\r\n'。在寫入以文本模式(默認模式)打開的文件時,請不要使用 os.linesep 作為行終止符,請在所有平臺上都使用一個 '\n' 代替。11、os.pathsep()描述:作業系統通常用於分隔搜索路徑(如 PATH)中不同部分的字符,如 POSIX 上是 ':',Windows 上是 ';'。在 os.path 中也可用。
12、os.close()fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )os.write(fd, bytes("This is test", encoding = "utf8"))os.close( fd )
13、os.stat()os.stat('C:/Users/wuzhengxiang/Desktop/股票數據分析\\pi.txt')os.stat_result(st_mode=33206, st_ino=22236523160361562, st_dev=2419217970, st_nlink=1, st_uid=0, st_gid=0, st_size=53, st_atime=1589638199, st_mtime=1589638199, st_ctime=1581868007)
14、os.sep()
描述:顯示當前平臺下路徑分隔符,在 POSIX 上是 '/',在 Windows 上是是 '\\'15、os.path.abspath()#Excel文件os.path.abspath('all_data.xlsx')'C:\\Users\\zhengxiang.wzx\\all_data.xlsx'#圖片文件os.path.abspath('IMG_7358.JPG')'C:\\Users\\zhengxiang.wzx\\IMG_7358.JPG'16、os.path.basename()描述:返回文件名,純粹字符串處理邏輯,路徑錯誤也可以語法:os.path.basename(path)os.path.basename('C:\\Users\\zhengxiang.wzx\\all_data.xlsx')'all_data.xlsx'17、os.path.commonprefix()描述:返回list(多個路徑)中,所有path共有的最長的路徑語法:os.path.commonprefix(list)os.path.commonprefix(['http://c.biancheng.net/python/aaa', 'http://c.biancheng.net/shell/'])'http://c.biancheng.net/'os.path.commonprefix(['http://bianc/python/aaa', 'http://c.biancheng.net/shell/'])'http://'
18、os.path.dirname()os.path.dirname('C://my_file.txt') 'C://'
os.path.dirname('C://python//my_file.txt')'C://python'19、os.path.exists()描述:如果路徑 path 存在,返回 True;如果路徑 path 不存在,返回 False。os.path.exists('C:/Users/wuzhengxiang/Desktop/股票數據分析/pi.txt')True
os.path.exists('C:/Users/wuzhengxiang/Desktop/股票數據分析/')True
os.path.exists('C:/Users/wuzhengxiang/Desktop/股票數據分析/pi_01.txt')Fals
20、os.path.lexists()
描述:路徑存在則返回True,路徑損壞也返回True, 不存在,返回 False。os.path.lexists('C:/Users/wuzhengxiang/Desktop/股票數據分析/pi.txt')True
os.path.lexists('C:/Users/wuzhengxiang/Desktop/股票數據分析/pi_01.txt')False
21、os.path.expanduser()描述:把path中包含的"~"和"~user"轉換成用戶目錄語法:os.path.expanduser(path)os.path.expanduser('~/wuzhengxiang/Desktop/股票數據分析/')'C:\\Users\\wuzhengxiang/wuzhengxiang/Desktop/股票數據分析/'
22、os.path.expandvars()描述:根據環境變量的值替換path中包含的"$name"和"${name}"語法:os.path.expandvars(path)os.environ['KITTIPATH'] = 'D:/thunder'path = '$KITTIPATH/train/2011_09_26_drive_0001_sync/proj_depth/velodyne_raw/image_02/0000000013.png'os.path.expandvars(path)'D:/thunder/train/2011_09_26_drive_0001_sync/proj_depth/velodyne_raw/image_02/0000000013.png'
23、os.path.getatime()描述:返回最近訪問時間(浮點型秒數),從新紀元到訪問時的秒數。語法:os.path.getatime(path)os.path.getatime('C:/Users/wuzhengxiang/Desktop/股票數據分析/pi.txt')1589638199.1343248
24、os.path.getmtime()描述:返回最近文件修改時間,從新紀元到訪問時的秒數。語法:os.path.getmtime(path)os.path.getmtime('C:/Users/wuzhengxiang/Desktop/股票數據分析/pi.txt')1583069050.8148942
25、os.path.getctime()描述:返回文件 path 創建時間,從新紀元到訪問時的秒數。語法:os.path.getctime(path)os.path.getctime('C:/Users/wuzhengxiang/Desktop/股票數據分析/pi.txt')1581868007.6123319
26、os.path.getsize()os.path.getsize('C:/Users/wuzhengxiang/Desktop/股票數據分析/test.gif')1128677
27、os.path.isabs()描述:判斷是否為絕對路徑,也就是說在WIndow系統下,如果輸入的字符串以" / "開頭,os.path.isabs()就會返回Trueos.path.isabs('D:/thunder')True
os.path.isabs('D:\thunder')False
os.path.isabs('D:\\thunder')True
28、os.path.isfile()#文件不存在 返回Falseos.path.isfile("C:/Users/wuzhengxiang/Desktop/股票數據分析/pi_01.txt")Falseos.path.isfile("C:/Users/wuzhengxiang/Desktop/股票數據分析/pi.txt")True#不是文件 返回Falseos.path.isfile("C:/Users/wuzhengxiang/Desktop/股票數據分析/")False
29、os.path.isdir()os.path.isdir('C:/Users/wuzhengxiang/Desktop/股票數據分析')True
os.path.isdir('C:/Users/wuzhengxiang/Desktop/股票數據分析1')False
os.path.isdir('C:/Users/wuzhengxiang/Desktop/股票數據分析/pi.txt')False
30、os.path.join()描述:把目錄和文件名合成一個路徑,1.如果各組件名首字母不包含』/』,則函數會自動加上,2.如果有一個組件是一個絕對路徑,則在它之前的所有組件均會被捨棄,3.如果最後一個組件為空,則生成的路徑以一個』/』分隔符結尾語法:os.path.join(path1[, path2[, ...]])os.path.join('C:/Users','wuzhengxiang/Desktop/','股票數據分析')'C:/Users\\wuzhengxiang/Desktop/股票數據分析'
Path1 = 'home'Path2 = 'develop'Path3 = 'code'
Path10 = Path1 + Path2 + Path3Path20 = os.path.join(Path1,Path2,Path3)print ('Path10 = ',Path10)print ('Path20 = ',Path20)
Path10 = homedevelopcodePath20 = home\develop\code31、os.path.normcase()語法:os.path.normcase(path)os.path.normcase('D:\Python\test\data.txt')'d:\\python\test\\data.txt'os.path.normcase('c:/WINDOWS\\system64\\')'c:\\windows\\system64\\'語法:os.path.normpath(path)os.path.normpath('c:'c:\\windows\\Temp'
33、os.path.realpath()語法:os.path.realpath(path)os.path.relpath('C:\\Users\\Administrat\\代碼TRY\\test.ipynb', '代碼TRY')'..\\..\\..\\..\\Administrat\\代碼TRY\\test.ipynb'
34、os.path.relpath()描述:返回從當前目錄或 start 目錄(可選)到達 path 之間要經過的相對路徑。這僅僅是對路徑的計算,不會訪問文件系統來確認 path 或 start 的存在性或屬性。語法:os.path.relpath(path[, start])os.path.relpath('C:/Users/wuzhengxiang/Desktop/股票數據分析\\test.gif')'test.gif'
35、os.path.samefile( )語法:os.path.samefile(path1, path2)os.path.samefile('C:\\Users', 'C:\\Users')Trueos.path.samefile('C:\\Users', 'C:/Users')Trueos.path.samefile('C:\\Users', 'C:/Users/wuzhengxiang')False
36、os.path.split()描述:把路徑分割成 dirname 和 basename,返回一個元組os.path.split('D:\Python\test\data.txt') ('D:\\Python\test', 'data.txt')
37、os.path.splitdrive()37、os.path.splitdrive()
描述:一般用在 windows 下,返回驅動器名和路徑組成的元組語法:os.path.splitdrive(path)os.path.splitdrive('C:/Users/zhengxiang.wzx/IMG_7358.JPG')('C:', '/Users/zhengxiang.wzx/IMG_7358.JPG')
38、os.path.splitext()語法:os.path.splitext(path)os.path.splitext('C:/Users/zhengxiang.wzx/IMG_7358.JPG')('C:/Users/zhengxiang.wzx/IMG_7358', '.JPG')
39、os.path.walk()描述:遍歷path,進入每個目錄都調用visit函數,visit函數必須有3個參數(arg, dirname, names),dirname表示當前目錄的目錄名,names代表當前目錄下的所有文件名,args則為walk的第三個參數語法:os.path.walk(path, visit, arg)list(os.walk(abs_cur_dir))[('C:/Users/wuzhengxiang/Desktop/股票數據分析', ['1122'], ['ETF研究.py', 'foo.txt', 'pi.txt', 'render.html', 'test.gif']), ('C:/Users/wuzhengxiang/Desktop/股票數據分析\\1122', [], [])]abs_cur_dir ='C:/Users/wuzhengxiang/Desktop/股票數據分析'file_url=[]for dirs,folders,files in os.walk(abs_cur_dir): for i in files: file_url.append(os.path.join(dirs,i)) file_url ['C:/Users/wuzhengxiang/Desktop/股票數據分析\\ETF研究.py', 'C:/Users/wuzhengxiang/Desktop/股票數據分析\\foo.txt', 'C:/Users/wuzhengxiang/Desktop/股票數據分析\\pi.txt', 'C:/Users/wuzhengxiang/Desktop/股票數據分析\\render.html', 'C:/Users/wuzhengxiang/Desktop/股票數據分析\\test.gif']本站qq群1003271085,加入微信群請回復「加群」
獲取一折本站知識星球優惠券,複製連結直接打開:
https://t.zsxq.com/yFQV7am
喜歡文章,點個在看