Home:ALL Converter>How to run another Python script in a different folder, which takes a csv file in its own path to read?

How to run another Python script in a different folder, which takes a csv file in its own path to read?

Ask Time:2020-12-02T22:49:36         Author:3Pac

Json Formatter

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.

Author:3Pac,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/65110417/how-to-run-another-python-script-in-a-different-folder-which-takes-a-csv-file-i
yy