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