Python file operations-sample code for merging text file content,

Source: Internet
Author: User

Python file operations-sample code for merging text file content,

Preface

I believe that when you first enter a project, you generally need to look at the code. Sometimes, I want to print the code file, but the number of code files is usually two or more digits. Opening and printing one by one is really too laborious. In addition, it may also be a waste of paper because a code file occupies only one or two lines. These problems can be solved if they can be merged into a text file.

Currently, a large number of functions are used: the content of multiple small files is merged into a unified file, and the original file is renamed to mark that it has been processed.
I used other scripts to write it. I tried to write it in python. By the way, I am familiar with python's file processing commands.

Original file

After processing

There is also the first carriage return because of indentation.

It includes File Creation and removal, file content reading and writing, file rename syntax commands, and so on.

Sample Code

#-*-Coding: UTF-8-*-import osimport timeimport datetimedef merge_file (file_path, file_name): # file_path must exits if (OS. path. exists (file_path) is False): print ('file _ path is not exists') return if (OS. path. exists (OS. path. join (file_path, file_name): OS. remove (OS. path. join (file_path, file_name) # '% Y _ % m _ % d % H % M % s', create a text file named by date targetfilename = pai'fobj = ope N (OS. path. join (file_path, targetfilename), 'w') fobj. close () # a opens the file with open (OS. path. join (file_path, targetfilename), 'A', encoding = 'gbk') as f_wirte: files = OS. listdir (file_path) for file in files: print (OS. path. join (file_path, file) with open (file_path + '\' + file, 'R', encoding = 'gbk') as f: for line in f. readlines (): if (line. strip (). _ len _ ()> 0: # exclude the empty line f_wirte.write (line) f_wirte. Write ('\ n') # after reading a file, add a carriage return. Otherwise, the last line of the first file and the first line of the second file do not have a carriage return. # After the file is merged, rename the original file, # OS. path. splitext (file) [0] extracts the file name, excluding the suffix # OS. path. splitext (file) [1] extracts the file suffix if (file! = Targetfilename): OS. rename (OS. path. join (file_path, file), OS. path. join (file_path, OS. path. splitext (file) [0] + 'in _' + str (time. strftime ('% Y % m % d % H % M % s') +' _ processed '+ '.txt') merge_file ('d: \ TestPythonMergeFile ', 'auoto _ create_a_category_file ')

Summary

The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.

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.