Python reads everything under a file, gets a filename, intercepts characters, writes back a file

Source: Internet
Author: User

# CODING=GBK
Import OS
Import Os.path
#读取目录下的所有文件, including nested folders
Def getfilelist (dir, fileList):
    Newdir = Dir
    if os.path.isfile (dir):
        Filelist.append (dir)
    Elif Os.path.isdir (dir):
         for  in Os.listdir (dir):
            # If you need to ignore some folders, use the following code
            if "xxx":
            Continue
            Newdir = Os.path.join (dir, s)
            Getfilelist (Newdir, FileList)
    return fileList
"E:\\differnernt_size_digit_data\\resize\\train\\28x28"
List = Getfilelist (Filedir, [])
# Open a file
fo = open ("file_list.txt""W")  # opening file
 for  in list:
    Print (i)  # Test full file path
    Print (Os.path.basename (i))  # file name
    index = I.find (".", 0)  # Find the location of the dot
    Print (I[index-1:index])  # intercept target characters
    " " + I[index-1:index])  # test Target string
    " " "\ n")  # write the target string to the file
Fo.close ()  # Close Open File

Python reads everything under a file, gets a filename, intercepts characters, writes back to a file

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.