0

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

1
  • use os or glob module to get a list of dirs. with json Commented Apr 23, 2018 at 12:49

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.