Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Copy file and rename it
#1
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!
Reply
#2
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
Reply
#3
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'
Reply
#4
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
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
I know its a mess, thats why I am asking for help
Not sure I understand your code..........
Thank you
Reply
#6
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
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#8
There is no need a function which just prints a variable.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#9
I was assuming the print was a place holder pending later development.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#10
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why is the copy method name in python list copy and not `__copy__`? YouHoGeon 2 276 Apr-04-2024, 01:18 AM
Last Post: YouHoGeon
  Copy Paste excel files based on the first letters of the file name Viento 2 442 Feb-07-2024, 12:24 PM
Last Post: Viento
  Rename first row in a CSV file James_S 3 584 Dec-17-2023, 05:20 AM
Last Post: James_S
  rename file RolanRoll 0 533 May-18-2023, 02:17 PM
Last Post: RolanRoll
  '' FTP '' File upload with a specified string and rename midomarc 1 1,163 Apr-17-2023, 03:04 AM
Last Post: bowlofred
  is it possible to copy image from email and place into excel file? cubangt 3 1,272 Nov-30-2022, 05:11 PM
Last Post: snippsat
  Please help me [copy and paste file from src to dst] midomarc 2 1,019 Nov-24-2022, 10:13 PM
Last Post: midomarc
  rename same file names in different directories elnk 0 715 Nov-04-2022, 05:23 PM
Last Post: elnk
  rename and add desire "_date" to end of file name before extention RolanRoll 1 1,242 Jun-13-2022, 11:16 AM
Last Post: gruntfutuk
  Rename Files based on XML file klturi421 3 2,203 Oct-22-2021, 07:37 PM
Last Post: klturi421

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020