473,396 Members | 1,849 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

copy and rename a file

18
Hey,
I have a problem. I was presented a problem where I need to open a file and then copy and rename it. We are still using really basic commands, but I am pretty lost with this. I know how to open a file and get python to read it. Any help would be great.
Mar 5 '07 #1
12 29947
bartonc
6,596 Expert 4TB
Hey,
I have a problem. I was presented a problem where I need to open a file and then copy and rename it. We are still using really basic commands, but I am pretty lost with this. I know how to open a file and get python to read it. Any help would be great.
Expand|Select|Wrap|Line Numbers
  1. fIn = open(r"filename")  # use raw strings for windows file names
  2. fOut = open(r"newFileName", "w")
  3. for line in fIn:
  4.     fOut.write(line)
  5. fIn.close()
  6. fOut.close()
This is for text files.
Mar 5 '07 #2
wocosc
18
Expand|Select|Wrap|Line Numbers
  1. fIn = open(r"filename")  # use raw strings for windows file names
  2. fOut = open(r"newFileName", "w")
  3. for line in fIn:
  4.     fOut.write(line)
  5. fIn.close()
  6. fOut.close()
This is for text files.

Cool, thanks.. Maybe its just me, but I do understand your code, however, I can not figure out how to insert a line where the user can enter the name of the new file.. But I will work on it. I really do appreciate your help..
Mar 5 '07 #3
wocosc
18
Nevermind, I got it working, but I can't figure out how to write it as a .txt instead of a file.

************************************************** ******************
Expand|Select|Wrap|Line Numbers
  1. def copy():
  2.     fIn = open(r"auston")  # use raw strings for windows file names
  3.     name= raw_input("Enter the new file name: ")
  4.     fOut = open(name, "w")
  5.     for line in fIn:
  6.         fOut.write(line)
  7.     fIn.close()
  8.     fOut.close()
Mar 5 '07 #4
wocosc
18
So what I need to figure now is for the program to prompt for the file
raw_input("What is the original file: ")
raw_input("What is the new name: ")

Which I know how to do, just not in this sense. But thanks for the original response so quickly, I do understand what that code does, I just dont know how to insert the about code to work. So if anyone can help with this conundrum, I would appreciate it a lot.
And this is not graded, it is just for out of class practice, but our book does not cover this.
Mar 5 '07 #5
ghostdog74
511 Expert 256MB
Hey,
I have a problem. I was presented a problem where I need to open a file and then copy and rename it. We are still using really basic commands, but I am pretty lost with this. I know how to open a file and get python to read it. Any help would be great.
what is your exact requirement? you just don't open a file and do nothing, then copy and rename it.
For purely renaming or copying files, you can use the shutil module
eg
Expand|Select|Wrap|Line Numbers
  1. import  shutil
  2. shutil.move(src,dst) #move file
  3. shutil.copy(src,dst) #copy file
  4.  
The os.rename() method also can rename files.
Mar 6 '07 #6
wocosc
18
My exact requirement is:
Write an interactive program Copy() that makes a copy of a
file. It should prompt the user for the name of the existing file
and then name for the new file that will be created. The new
file should be an exact duplicate of the original.
Mar 6 '07 #7
ghostdog74
511 Expert 256MB
My exact requirement is:
Write an interactive program Copy() that makes a copy of a
file. It should prompt the user for the name of the existing file
and then name for the new file that will be created. The new
file should be an exact duplicate of the original.
well, if that's the exact requirement, then use shutil will do. It does the bulk of the the copy job for you.
eg
Expand|Select|Wrap|Line Numbers
  1. import shutil
  2. srcfile = raw_input("What is your file name: ")
  3. dstfile = raw_input("What do you want to rename %s to?" %srcfile)
  4. try:
  5.    shutil.copy(srcfile,dstfile)
  6. except Exception,e:
  7.    print e
  8.  
Mar 6 '07 #8
bartonc
6,596 Expert 4TB
Nevermind, I got it working, but I can't figure out how to write it as a .txt instead of a file.

************************************************** ******************
Expand|Select|Wrap|Line Numbers
  1. def copy():
  2.     fIn = open(r"auston")  # use raw strings for windows file names
  3.     name= raw_input("Enter the new file name: ")
  4.     fOut = open(name, "w")
  5.     for line in fIn:
  6.         fOut.write(line)
  7.     fIn.close()
  8.     fOut.close()
I've added code tags to your post. Please read the "POSTING GUIDELINES" on the right hand side of this page to learn how to do that. Thanks.
Expand|Select|Wrap|Line Numbers
  1. def copy():
  2.     inFileName = raw_input("Enter the original file name: ") 
  3.     print inFileName    # do this to "debug" and remove after it works
  4.     outFileName = raw_input("Enter the new file name: ") + ".txt"
  5.     print outFileName   # do this to "debug" and remove after it works
  6.     fIn = open(inFileName)  # use raw strings for windows file names
  7.     fOut = open(outFileName, "w")
  8.     for line in fIn:
  9.         fOut.write(line)
  10.     fIn.close()
  11.     fOut.close()
You won't need raw strings unil you start using a directory (folder) other than the one that you program is in.
Mar 6 '07 #9
bartonc
6,596 Expert 4TB
well, if that's the exact requirement, then use shutil will do. It does the bulk of the the copy job for you.
eg
Expand|Select|Wrap|Line Numbers
  1. import shutil
  2. srcfile = raw_input("What is your file name: ")
  3. dstfile = raw_input("What do you want to rename %s to?" %srcfile)
  4. try:
  5.    shutil.copy(srcfile,dstfile)
  6. except Exception,e:
  7.    print e
  8.  
My friend ghostdog74 knows the module library better than anyone I know. Very cool, GD
Mar 6 '07 #10
wocosc
18
Hey, Thanks y'all... Y'all have been a tremendous help in such a short time..
Mar 6 '07 #11
ghostdog74
511 Expert 256MB
My friend ghostdog74 knows the module library better than anyone I know. Very cool, GD
:) well, i am a system admin by job nature, so i use this module quite a lot copying and moving files in my script. :)
Mar 6 '07 #12
bartonc
6,596 Expert 4TB
Hey, Thanks y'all... Y'all have been a tremendous help in such a short time..
You are welcome. That's what we're here for. Keep posting,
Barton
Mar 6 '07 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: N. Schultheiss | last post by:
I'm new at this and wonder if this is possible. I'm running a Win2k server with FP 2002 Extensions using discussion boards. There is a time delay in the posted content and the way our moderators...
3
by: G. Dean Blake | last post by:
In a ASP.NET Visual Studio project I have a webform named FinResults_AR.aspx and its associated vb and resx files. I want to copy and rename to FinResults_GL so that I have an exact duplicate...
11
by: MarkusR | last post by:
Good day, I need to safely copy files from one directory to another. I need to make sure that I do not lock the file making it unreadable by others. I am not sure if this is the default...
12
by: mantrid | last post by:
Im trying to move a file but am having luck my code is below. The temp and target paths are valid as they echo correctly. but I cant get the copy() function to work, or the rename() function ...
13
by: Avi | last post by:
Hi, Is there a UNIX C system command that will let me copy a file? I am looking for something similar to "cp" that can be called within a C program. I know of the "link" system call but this...
25
by: Andy_Khosravi | last post by:
I just recently changed my database that I'm running from a monolithic DB to a split FE/BE. The front end resides on the client machine and the BE resides on a network drive. I'm experimenting with...
9
by: =?Utf-8?B?VGVycnk=?= | last post by:
Hi, Is it possible (w/o generating a gazillion errors) to copy a form in VS 2005 so that it can be modified and reused. In other words, I have a form, alot of which I wish to use in a different...
4
by: Tom | last post by:
Every week our web guys publish the web site to all of our servers by logging into the server and doing a copy - paiste. We want to elminate the developers from logging into each server and doing...
4
by: jodleren | last post by:
While reading php.net I found " bool copy ( string $source , string $dest ) Makes a copy of the file source to dest . If you wish to move a file, use the rename() function. " ..... what?...
1
by: ogo796 | last post by:
hi everyone i upload files everyday to the server using a php coded program , it works from other computers but not on the others. Could this be a problem with http headers of specific computers...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.