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.
12 29947
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.
-
fIn = open(r"filename") # use raw strings for windows file names
-
fOut = open(r"newFileName", "w")
-
for line in fIn:
-
fOut.write(line)
-
fIn.close()
-
fOut.close()
This is for text files.
-
fIn = open(r"filename") # use raw strings for windows file names
-
fOut = open(r"newFileName", "w")
-
for line in fIn:
-
fOut.write(line)
-
fIn.close()
-
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..
Nevermind, I got it working, but I can't figure out how to write it as a .txt instead of a file.
************************************************** ****************** -
def copy():
-
fIn = open(r"auston") # use raw strings for windows file names
-
name= raw_input("Enter the new file name: ")
-
fOut = open(name, "w")
-
for line in fIn:
-
fOut.write(line)
-
fIn.close()
-
fOut.close()
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.
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 -
import shutil
-
shutil.move(src,dst) #move file
-
shutil.copy(src,dst) #copy file
-
The os.rename() method also can rename files.
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.
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 -
import shutil
-
srcfile = raw_input("What is your file name: ")
-
dstfile = raw_input("What do you want to rename %s to?" %srcfile)
-
try:
-
shutil.copy(srcfile,dstfile)
-
except Exception,e:
-
print e
-
Nevermind, I got it working, but I can't figure out how to write it as a .txt instead of a file.
************************************************** ****************** -
def copy():
-
fIn = open(r"auston") # use raw strings for windows file names
-
name= raw_input("Enter the new file name: ")
-
fOut = open(name, "w")
-
for line in fIn:
-
fOut.write(line)
-
fIn.close()
-
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. -
def copy():
-
inFileName = raw_input("Enter the original file name: ")
-
print inFileName # do this to "debug" and remove after it works
-
outFileName = raw_input("Enter the new file name: ") + ".txt"
-
print outFileName # do this to "debug" and remove after it works
-
fIn = open(inFileName) # use raw strings for windows file names
-
fOut = open(outFileName, "w")
-
for line in fIn:
-
fOut.write(line)
-
fIn.close()
-
fOut.close()
You won't need raw strings unil you start using a directory (folder) other than the one that you program is in.
well, if that's the exact requirement, then use shutil will do. It does the bulk of the the copy job for you.
eg -
import shutil
-
srcfile = raw_input("What is your file name: ")
-
dstfile = raw_input("What do you want to rename %s to?" %srcfile)
-
try:
-
shutil.copy(srcfile,dstfile)
-
except Exception,e:
-
print e
-
My friend ghostdog74 knows the module library better than anyone I know. Very cool, GD
Hey, Thanks y'all... Y'all have been a tremendous help in such a short time..
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. :)
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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
...
|
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...
|
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...
|
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...
|
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...
|
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?...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
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...
|
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,...
|
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,...
|
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...
|
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...
|
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,...
| |