0

I have the following code in python, which I use to read all the files in the specified path, and process them all by the C++ source code.

import os
import os.path
import subprocess

path = '/Users/'

dirs = os.listdir(path)

for path in dirs:
    if 'whatever' in path and not os.path.splitext(path)[1]:
        proc = subprocess.Popen(["./cppSourceCode", path])

It works fine if the files (which are the input to my C++ code) are all in the same folder with C++ source code, and the output would be also in the same folder.

However, what I want to do is to put the data files in a folder (e.g. Data Folder), the output should go to a different folder (e.g. The output) while my C++ and Python codes are in their folder (e.g. Codes folder)

How this could be done? How to make this doable using 3 different folders?

philippos
  • 1,142
  • 5
  • 20
  • 41
  • how would you do this without Python ? And then do the same with Python. – furas Jan 04 '17 at 16:05
  • so basically you need your cpp executable to be in the same directory as your python script or there's more to it? – Jean-François Fabre Jan 04 '17 at 16:05
  • @furas I do it all with Python. Sorry but couldn't understand your point. – philippos Jan 04 '17 at 16:08
  • @Jean-FrançoisFabre Exactly, I want the cpp executable to be in the same directory as my python script. And the files that the cpp reads are in different folder, and then the output to be in another folder too. – philippos Jan 04 '17 at 16:10
  • For the input to live in a different folder use the absolute path to the input file. Where the output goes is defined by your C++ code so you need to modify this code accordingly. – a_guest Jan 04 '17 at 16:10
  • try to run `cppSourceCode` directly in command line. Can you change directory when you run it in command line ? How will you do this ? If you know how to do this in command line then do the same when you use it in Python. – furas Jan 04 '17 at 16:11
  • @furas I can ran the C++ code from the command line, which I can do it to process files one by one. However, I use the python script to process all the files in the folder at once. – philippos Jan 04 '17 at 16:13
  • @a_guest can you please give me an example on how to set the output folder from the cpp code? any useful links? – philippos Jan 04 '17 at 16:15
  • problem is if `cppSourceCode` can do something like `cppSourceCode --input_folder some_folder --output_folder other_folder` or something similar. – furas Jan 04 '17 at 16:15
  • Possible duplicate of [Find current directory and file's directory](http://stackoverflow.com/questions/5137497/find-current-directory-and-files-directory) – Jean-François Fabre Jan 04 '17 at 16:15
  • or maybe normally in command line you had to do `cd output_folder ; ../cppSourceCode input_folder/some_file` – furas Jan 04 '17 at 16:19
  • @PhilipHamoui In order to set the output folder just use an absolute path to it. Where the path comes from is a different story, you could pass it as a parameter to the C++ code. – a_guest Jan 04 '17 at 16:22

0 Answers0