Python file Operation _ Copy Copy _ code implementation

Source: Internet
Author: User

Requirements:

1, copy operation of existing files

2, the copied file after the filename plus [copy]

3, file comparison how to optimize processing

1 #-*-coding:utf-8-*-2 #This is written under Python 2, using the Raw_input3 #Old_file_name used to receive the input file name4Old_file_name = Raw_input ('Please enter the file name you want to copy:')5 #Open the input file6Old_file = open (Old_file_name,'R')7 8 #The name of the new file is processed, preceded by the suffix. Plus [copy]9 #disassemble the Old_file_name, add [Copy], and mergeTennum = Old_file_name.rfind ('.')#Find's got a subscript. OneLeft_part = Old_file_name[:num]#the left half of the. ARight_part = Old_file_name[num:]#the right half of the. -New_file_name = Left_part +'[Copy parts]'+Right_part -  theNew_file = open (New_file_name,'W')#open a new file because it does not exist, use ' W ' to name the build -Content = Old_file.readline ()#read a row - #Loop, read one line of old files at a time, until content=0 is no content. -  whileLen (content) >0: + new_file.write (content) -Content =Old_file.readline () + #don't forget to close the file A old_file.close () atNew_file.close ()

Here are some of the issues encountered in the implementation process:

1, this is implemented under Linux, the file is on the desktop, if implemented under Windows Pycharm, how to handle the file path problem "Pending"

There's no raw_input in 2,python 3.

3, the operation of the string is very important, to more practice more read more remember more knock

Python file Operation _ Copy Copy _ code implementation

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.