Vidya Marathe
Vidya Marathe

Reputation: 443

Copying files from multiple folder to another folder

I have a folder called Reports with multiple folders ID1, ID2, ID3...and so on. Each of these folders have a json report. Now I want to copy all these json reports into a single folder called Input

import os 
import sys
import shutil
list={} 
list=os.system("find /home/admin1/Report -name '*.json'")
print list
for i in list:
    os.system('cp i /home/admin1/Input')

This gives error: TypeError: 'int' object is not iterable

Upvotes: 0

Views: 31

Answers (1)

Tom Dalton
Tom Dalton

Reputation: 6190

There's quite a few issues here.

  1. You're redefining Python's builtin list function, and defining it as a variable, containing an empty dictionary (which is not even a list).

  2. You then throw away that empty dictionary and redefine list as the result of os.system("find /home/admin1/Report -name '*.json'"). That's not gonig to do what you want, because os.system returns an integer (https://docs.python.org/3/library/os.html#os.system). It looks like you were expecting it to return a list of results.

  3. You're then trying to use the for loop to iterate over that integer, which is what's giving you the TypeError.

  4. os.system('cp i /home/admin1/Input') (which your program never gets to due to the error above) literally runs cp i /home/admin1/Input, you're not substituting "i" for the value of variable i.

Rather than using os.system to run find, it would arguably be better to use Python's os.walk (see https://www.pythoncentral.io/how-to-traverse-a-directory-tree-in-python-guide-to-os-walk/) to work through the directory tree yourself, rather than trying to manually parse the output of find.

Upvotes: 1

Related Questions