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
python One click download vide