top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

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

+2 votes

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 =
 out_file = open("desktop/newfolder/%s.txt", 'w') % file

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


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

The problem is the above line. Rewrite is thus:

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


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 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",""],stdout=PIPE)

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

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

Contact Us
+91 9880187415
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Karnataka INDIA.