![]() |
Copy file and rename it - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Copy file and rename it (/thread-2063.html) Pages:
1
2
|
Copy file and rename it - Filthy_McNasty - Feb-15-2017 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! RE: Copy file and rename it - Larz60+ - Feb-15-2017 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/123198/how-do-i-copy-a-file-in-python RE: Copy file and rename it - Filthy_McNasty - Feb-15-2017 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' RE: Copy file and rename it - wavic - Feb-15-2017 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 RE: Copy file and rename it - Filthy_McNasty - Feb-15-2017 I know it s a mess, that s why I am asking for helpNot sure I understand your code.......... Thank you RE: Copy file and rename it - wavic - Feb-15-2017 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 RE: Copy file and rename it - ichabod801 - Feb-15-2017 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.
RE: Copy file and rename it - wavic - Feb-15-2017 There is no need a function which just prints a variable. RE: Copy file and rename it - ichabod801 - Feb-16-2017 I was assuming the print was a place holder pending later development. RE: Copy file and rename it - Filthy_McNasty - Feb-16-2017 (Feb-15-2017, 10:58 PM)wavic Wrote: This is just an IPython interpreter. Like the regular one with extras |