Note the Linux host
Delete files in the specified directory with the file name suffix. tmp
1 #!/usr/bin/python2 ImportOS, Glob3DirName ='/ tmp'4allpy = Glob.glob (Os.path.join (dirname,'*'))5 forFileNameinchallpy:6FileSize =os.path.getsize (filename)7 if(Filename.endswith ('tmp')):8 Try:9 os.remove (filename)Ten except: OneContinue
Output the largest two files in a specified folder (including subfolders)
1 #!/usr/bin/python2 ImportOS3DirName ='/ tmp'4Allsize = []5 for(Root,dirs,files)inchOs.walk (dirname):6 forFileNameinchFiles:7FullName =Os.path.join (root,filename)8FileSize =os.path.getsize (FullName)9 Allsize.append ((filesize,fullname))Ten Allsize.sort () One PrintAllsize[-2:]
Delete all files and folders in the specified file (including subfolders)
1 #!/usr/bin/python2 ImportOS3DirName ='/ tmp'4Allsize = []5 for(Root,dirs,files)inchOs.walk (dirname):6 forFileNameinchFiles:7FullName =Os.path.join (root,filename)8FileSize =os.path.getsize (FullName)9 Allsize.append ((filesize,fullname))Ten Allsize.sort () One PrintAllsize[-2:]
Python Snippet (i)