Python handles files in a folder (create a new file to save), Listdir sequential output

Source: Internet
Author: User

1. Original file

Required Files:

#-*-Coding:utf8-*-ImportOSdefsplitsentence (inputfile,name): Fin= Open (Inputfile,'R')#open a file in a read way    Printname Fout= Open ('/home/xdj/abstract1/'+name,'W')#open a file in the way it was written     forEachlineinchFin:if 'abstract=' incheachLine:fout.write (eachline)#writes the result of the word breaker to the output file.fin.close () fout.close () path='/home/xdj/abstract'FNS=[os.path.join (ROOT,FN) forRoot,dirs,filesinchOs.walk (PATH) forFninchFiles]i=-1; forFinchfns:i=i+1splitsentence (f,files[i])

root for '/home/xdj/abstract'

Dirs is empty

Files as 'abstract' in

# -*-Coding:utf8-*- Import operator  from Import Listdir Import os fileList=os.listdir ('/home/xdj/abstract') filelist.sort () Print fileList

[' 2007.BibTeX ', ' 2008.BibTeX ', ' 2009.BibTeX ', ' 2010.BibTeX ', ' 2011.BibTeX ', ' 2012.BibTeX ', ' 2013.BibTeX ', ' 2014. BibTeX ', ' 2015.BibTeX ']

Before sort ()

[' 2012.BibTeX ', ' 2013.BibTeX ', ' 2008.BibTeX ', ' 2009.BibTeX ', ' 2014.BibTeX ', ' 2010.BibTeX ', ' 2007.BibTeX ', ' 2011. BibTeX ', ' 2015.BibTeX ']

############### #33

Sort () is matched from left to right, not simple comparison size

Python handles files in a folder (create a new file to save), Listdir sequential output

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.