Copy of python files

Copy of python files


                                        

                                            
 import 
                                                os
                                            

                                            
 old_file_name 
                                                = input
                                                    ("Please input what's file do you want to copy go:"
                                                        )
                                            

                                            
 fp 
                                                = open
                                                    (old_file_name)
                                            

                                            
 content 
                                                = fp.read()                                                    

                                            

                                            index 
                                                = old_file_name.rfind(
                                                    '.')                                                        

                                            

                                            new_file_name 
                                                = old_file_name[:index]
                                                    +"[ ]"
                                                        +old_file_name[index:]                                                            

                                            

                                            dp 
                                                = open
                                                    (new_file_name,'w'
                                                        )
                                            

                                            
dp.write(content)

                                        

                                     

The above code is a copy of the file, our idea is like this:

You can open an existing file, read the content of the file, and then create a new file. The name of the file is the old file name with the font like [Copy] appended to the end. Then write the content we just read in the old file to the new file. Just close the two files.

The second line allows the user to enter the file you want to copy. This file must exist, and preferably an absolute path.

The fourth line is to open the old file we want, and use the content variable to save the content in the old file

The fifth line is to find the subscript of the rightmost'.' symbol in the string of the old_file_name variable.

The sixth line is to name the new file, and then assign it to the variable new_file_name

Then there is a problem above. If we want to copy a file that you don t know how big, do not use read, because read will read all the content into the memory. If the file is too large, your memory will collapse. Don't use readlines because if your file contains only one line and the data in this line is large, your memory will also be affected.

You can use the following code:


                                        

                                            
 import 
                                                os
                                            

                                            
 old_file_name 
                                                = input
                                                    ("Please input what's file do you want to copy go:"
                                                        )
                                            

                                            
 fp 
                                                = open
                                                    (old_file_name)
                                            

                                            
 index 
                                                = old_file_name.rfind(
                                                    '.')                                                        

                                            

                                            new_file_name 
                                                = old_file_name[:index]
                                                    +"[ ]"
                                                        +old_file_name[index:]                                                            

                                            

                                            dp 
                                                = open
                                                    (new_file_name,'w'
                                                        )
                                            

                                            
 while 
                                                True:                                                    

                                            

                                                
                                                content = 
                                                    fp.read(1024
                                                        )
                                            

                                            
     
                                                if len
                                                    (content) =
                                                        = 0
                                                            :
                                            

                                            
         
                                                break
                                            

                                            

                                                
                                                dp.write(content)
                                            

                                            
fp.close()

                                            
dp.close()

                                        

                                     

The eighth line of the above code is to read the first 1024 characters of the file, and then determine whether the read content is empty, if it is, break and exit the loop, if not, write the content into the new file


This article is transferred from Zhou Ziqi s 51CTO blog, the original link: http://blog.51cto.com/izhouyu/1967644