0

I have a mainprogram called: onescripttorulethemall.py(Let us call this onescript.py)

I have a different folder(call it 'SCRIPTSTOBERULED'), located in a different path than my mainprogram. In this folder, there is also an other folder(lets call it folder_1) and this folder contains one python script(csvwriter_1.py) and one csv file. What this python script(csvwriter_1.py) do is basicaly, it reads the csv file and write it into an other csv file after creating it.

This is the code that I have in my mainprogram(onescript.py):

user='hkaan'
python_projects_folder='PycharmProjects'
scripts_to_be_ruled_folder='SCRIPTSTOBERULED'


path=f'C:\\Users\\{user}\\{python_projects_folder}\\{scripts_to_be_ruled_folder}'
star=os.listdir(path)
#THIS PRINTS THE FIRST ELEMENT IN 'SCRIPTSTOBERULED' FOLDER, WHICH IS FOLDER_1 
print(str(star[0])) 

#THIS IS THE PATH TO FOLDER_1
path2=f'C:\\Users\\{user}\\{python_projects_folder}\\{scripts_to_be_ruled_folder}\\{str(star[0])}'
python_script=os.listdir(path2)

#THIS IS THE PATH TO CSVWRITER_1.
python_path=f'C:\\Users\\{user}\\{python_projects_folder}\\{scripts_to_be_ruled_folder}\\{str(star[0])}\\{python_script[0]}'
#THIS RUNS THE CSVWRITER_1.PY
exec(open(python_path).read())

#I TRIED THE OS.SYSTEM CODE BELOW TOO, BUT IT DID NOT WORK.
#os.system(python_path)

And this is the code I have in my csvwriter_1.py:

deneme='wololo.csv'
resto_hrefs=[]
with open(deneme, 'r', encoding='UTF-8') as rd:
    reader = csv.reader(rd)
    for lines in reader:
        resto_hrefs.append(lines[0])
file_name='file.csv'
text=['kkkkkkkkk']
resto_hrefs.append(text)
with open(file_name, 'w', encoding='UTF-8', newline="") as csv_file:
    wr = csv.writer(csv_file)
    wr.writerows(resto_hrefs)

Please do not mind the un-meaningful words and the method. If I run csvwriter1.py, it gives me an output(in other words, successfully finishes running) because wololo.csv in its path. However, when I run my mainprogram(onescript.py), it gives me the error "No such file or directory: 'wololo.csv'". This error is surely expected, I want to learn how to overcome this issue.

Edit: The end goal I want to achive is, I want to run multiple scripts(like csvwriter_1) in different folders by running only my mainprogram. So the csv file and python scripts name will be different but similar in each folder. Thus, I want to write an efficient script in my mainprogram, not just copy/paste file and script names and run them.

3Pac
  • 29
  • 3
  • 1
    Use the full path for `wololo.csv` – Maurice Meyer Dec 02 '20 at 14:59
  • Python (or really the OS) doesn't particularly care where the script is stored; it cares about your current working directory. If you run `python -c 'open("filename")'` it will try to open `filename` in the current directory. You can store the same script in `/tmp/script.py` and it still behaves the same with `python /tmp/script.py` – tripleee Dec 02 '20 at 15:04
  • Does this answer your question? [How do I get the path and name of the file that is currently executing?](https://stackoverflow.com/questions/50499/how-do-i-get-the-path-and-name-of-the-file-that-is-currently-executing) – Tomerikoo Dec 02 '20 at 16:04
  • Or just use [`os.chdir`](https://docs.python.org/3/library/os.html#os.chdir) – Tomerikoo Dec 02 '20 at 16:07
  • I know that triplee. What I have now is several python scripts located in different folders. Each python scripts will read different csv files and put their output csv files in their respective directory. I basicaly want to create a start button for all those script running one by one like I run them manually. Of course, now, I started trying to run a single script(csvwriter_1.py) by my mainprogram(onescript.py). – 3Pac Dec 02 '20 at 16:07
  • Also see [What is the best way to call a script from another script?](https://stackoverflow.com/questions/1186789/what-is-the-best-way-to-call-a-script-from-another-script) that `exec(open(python_path).read())` is really non-idiomatic – Tomerikoo Dec 02 '20 at 16:08

0 Answers0