Python Forum

Full Version: Copy file and rename it
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I have to copy already created text file, Layer_Name.txt , so new copied file has to be renamed with user input initials (IN_Layer_Name.txt)
If the user does not enter a value then repeat the prompt until they do so.
The code that creates the copy has to be in a separate custom function that is created. The values for the name of the file to copy and the name of the file to create (i.e. the output) must be passed to custom function.

This is what I have done but it doesn`t work:

 
    import os
     root_path = r"C:\ABC"
    
     user_input = None
    
     def initials():
         print()
     
    while(not user_input):
        user_input = raw_input("Please enter your initials: ")
        initials(user_input)
        
    
    from shutil import copyfile
    initials = userinput
    copyfile('Layer_Names.txt', '%s_Layer_Names.txt' %initials)  
    break  
       
    print 'Copy file has been created'  


 
Thank you very much for your help!
I have used shutil for this - see: https://docs.python.org/2/library/shutil.html
This will copy individual files, or entire directories and subdirectories.
for a qick example, see: http://stackoverflow.com/questions/12319...-in-python
That link doesn`t answer my main problem, loop that ask user to enter a value and repeat the prompt until they do so.

This script just copy the file but it`s not what I need.


import os
root_path = r"C:\ABC"

userinput = raw_input("Please enter your initials:")

from shutil import copyfile
initials = userinput
copyfile('Layer_Names.txt', '%s_Layer_Names.txt' %initials)    

print 'Copy file has been created'
Its work here:
In [1]: usr_input = None

In [2]: while not usr_input:
   ...:     usr_input = raw_input("Initials: ")
   ...:     
   ...:     
Initials: 
Initials: 
Initials: 
Initials: 
Initials: 
Initials: 
In your original code the indentation is a mess
I know its a mess, thats why I am asking for help
Not sure I understand your code..........
Thank you
This is just an IPython interpreter. Like the regular one with extras
In Python the indentation defines the code blocks like the brackets in the most of the other programming languages.
Four spaces are used for indentation. So the while loop looks like this

from shutils import copyfile

# these three lines will be executed until some input
while not user_input:
    user_input = raw_input(Your initials: ")
    print(user_input)

initials = user_input
copyfile('Layer_Names.txt', '%s_Layer_Names.txt' %initials)
print 'Copy file has been created' 
I do not include some lines of you code because they are not used. For example os module or root_path variable
The main problem I see is initials = userinput. This overwrites the initials function that you defined. Also, it's user_input not userinput, so that line should give you a name error. I would remove that line, and then change the copyfile call to use user_input instead of initials. That should fix your code.
There is no need a function which just prints a variable.
I was assuming the print was a place holder pending later development.
(Feb-15-2017, 10:58 PM)wavic Wrote: [ -> ]This is just an IPython interpreter. Like the regular one with extras
In Python the indentation defines the code blocks brackets in the most of the other programming languages.
Four spaces are used for indentation. So the while loop looks like this

from shutils import copyfile

# these three lines will be executed until some input
while not user_input:
    user_input = raw_input(Your initials: ")
    print(user_input)

initials = user_input
copyfile('Layer_Names.txt', '%s_Layer_Names.txt' %initials)
print 'Copy file has been created' 
I do not include some lines of you code because they are not used. For example os module or root_path variable

Again, I am confused, is this just a part of the code suppose to be inserted somewhere in my script because as it is right now, it does not work.
Thank you
Pages: 1 2