top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Python: Creating a function for copying all files of a directory to another.

+2 votes
348 views

I want to take the file, "desktop/test.txt" and write it to "desktop/newfolder/test.txt". I tried the below script, and it gave me: "IOError: [Errno 2] No such file or directory: 'desktop/%s.txt'". Any suggestions would be great.

def firstdev(file):
 in_file = open("desktop/%s.txt") % file
 indata = in_file.read()
 out_file = open("desktop/newfolder/%s.txt", 'w') % file
 out_file.write(indata)
 out_file.close()
 in_file.close()

firstdev("test")
posted Nov 11, 2013 by Sanketi Garg

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote

You're using the % operator, which does your interpolations, at the
wrong point. You want to be adjusting the file name, not adjusting the
opened file:

 in_file = open("desktop/%s.txt" % file)
 out_file = open("desktop/newfolder/%s.txt" % file, 'w')
answer Nov 11, 2013 by Dewang Chaudhary
+1 vote

1)

in_file = open("desktop/%s.txt") % file

The problem is the above line. Rewrite is thus:

in_file = open("desktop/%s.txt" % file)

2)

out_file = open("desktop/newfolder/%s.txt", w) % file

Same here:

out_file = open("desktop/newfolder/%s.txt"% file, w)
answer Nov 11, 2013 by Satish Mishra
Similar Questions
+5 votes

I am building my script. I want to run all the test scripts.

Currently I am running the code "python setup.py test", it is running only the some tests in my directory. I want to run all the tests in my directory.

+3 votes

I am trying to count the number of files in a folder with python script but not getting the appropriate script. In which I can change the path and can find out the count of other folder files.

It doesn't matter for me how long the script is but need the clear script and it would be better if you can explain it also.

+1 vote

I have about 500 search queries, and about 52000 files in which I have to find all matches for each of the 500 queries.

How should I approach this? Seems like the straightforward way to do it would be to loop through each of the files and go line by line comparing all the terms to the query, but this seems like it would take too long.

Can someone give me a suggestion as to how to minimize the search time?

+2 votes

I want to use the stdout of child process as an input of another thread, but some how I am not able to read the stdout. Using Popen I have created a child process and stdout of it I have redirected to PIPE (because I don't want that to be printed on with my main process). Now using this statement,"Line=Proc.stdout.readline()" I want to read stdout of child process and I have to do operation on that. but somehow i am not able to read from stdout of child process.

Following is the code snapshot -

Proc = Popen(["python.exe","filename.py"],stdout=PIPE)

while True:
            Line = Proc.stdout.readline()
            print Line

Here,
Filename.py(Child process) is continuously printing "Hello World!!" in place of reading stdout of child process.

...