程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python standard library OS learning and use record

編輯:Python

Python Standard library os Usage record

Today I see a piece of code :

video_name = os.path.splitext(os.path.basename(args.input))[0]
frame_folder = os.path.join('tmp_frames', video_name)
os.makedirs(frame_folder, exist_ok=True)

There's a lot in it os Usage of , Make a note of
( First attach a detailed description of the reprint :os.path Common path operations )

os.path.basename: Find the last one "/" The elements behind , I did some experiments ( Notice that the last line is ‘’)

>>> os.path.basename('/media/qiaohui/Real-ESRGAN/CODE_OF_CONDUCT.md')
'CODE_OF_CONDUCT.md'
>>> os.path.basename('/media/qiaohui/Real-ESRGAN')
'Real-ESRGAN'
>>> os.path.basename('/media/qiaohui/Real-ESRGAN/')
''

os.path.splitext: Separate file name and extension , Don't talk much. Picture above

>>> os.path.splitext('CODE_OF_CONDUCT.md')
('CODE_OF_CONDUCT', '.md')
>>> os.path.splitext('model.pth.tar')
('model.pth', '.tar')
>>> os.path.splitext('model.pth')
('model', '.pth')

So look back at the first line of code

video_name = os.path.splitext(os.path.basename(args.input))[0]

First use basename obtain XXX.mp4, And then use splitext obtain (‘XXX’,‘.mp4’),[0] obtain ’XXX’ Is what you want video name

next os.path.join I don't think I need to introduce too much , Auto composition path , That is, it will automatically add a ’/‘, Forget it, let's have a picture

>>> os.path.join('tmp_frames','XXX')
'tmp_frames/XXX'

And finally os.makedirs, I just saw this usage yesterday , All I knew before was os.mkdir, Or am I ignorant ..
The difference is just os.makedirs Create multi-level directory ,os.mkdir Create a single level directory , If only one is built, for example ,666 Folder , It only needs to os.mkdir(‘666’); If you want to build multi-level , For example 666 Next build a 888, That's it makedirs(‘666/888/’), Behind the exist_ok If the parameter is True, If the file already exists, no error will be reported , If it is False You're going to report a mistake


Later, I met others os Usage will continue to be updated here


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved