-1

I am trying to use a subprocess in python to run some scripts on linux, but my path does not work on what I am trying to do..

I am using:

subprocess.Popen(["nohup", "python", DIR4, DIR2 + dirname + /*  + '/*.json'])

DIR4 = path of python, DIR2+dirname is the directory I want to go into. From here I want to use all sub directories in DIR2+dirname and all json files in all the subdirectories.

so for example:

 DIR2+dirname = /tmp/test/ 

in the /tmp/test/ directory, there are /tt1, /tt2, /tt3 each /tt directories contains 1.json, 2.json, 3.json How can I call this all using my command all the way on top?

HavelTheGreat
  • 3,299
  • 2
  • 15
  • 34
PETER
  • 59
  • 1
  • 9
  • Do you want to find the json files in all subdirectories recursively, or just in the directory plus one level of subdirectories? – Michael Jaros Mar 11 '15 at 20:20

2 Answers2

0

Use the accepted answer to this StackOverflow question, starting with DIR2+dirname as the path to find all your JSON files.

This snippet was copied from there and slightly adapted:

import fnmatch
import os

matches = []

for root, dirnames, filenames in os.walk(DIR2 + dirname):
  for filename in fnmatch.filter(filenames, '*.json'):
      matches.append(os.path.join(root, filename))

subprocess.Popen(["nohup", "python", DIR4] + matches)

If you have many files, keep in mind that there is an OS-specific limit on command line length, see this ServerFault question.

Community
  • 1
  • 1
Michael Jaros
  • 4,586
  • 1
  • 22
  • 39
0

Use the glob module:

import os.path
import glob
jsons = glob.glob(os.path.join(DIR2, dirname, '*', '*.json'))
subprocess.Popen(["nohup", "python", DIR4] + jsons)
tzaman
  • 46,925
  • 11
  • 90
  • 115
  • I am getting `Usage: python LIMSQCstats.py inputFile.json` and not doing what it should do @tzaman – PETER Mar 11 '15 at 20:39