Home:ALL Converter>python script output to be saved in different folder

python script output to be saved in different folder

Ask Time:2019-07-24T03:00:32         Author:Sundios

Json Formatter

I'm trying to build a keyword tool. For this, I built a python script that when you run it, it outputs a CSV file with the keyword, the ranking, the URL and the date.

I want to run more than one keyword and I want to save the output in different folders.

I created 5 different folders with my python script and I created a bash file that runs the script with different keywords and outputs different CSV files.

The bash file looks like this:

#! /bin/bash

/usr/bin/python3 /kw1/rank.py [website] [keyword1] 
sleep 30
/usr/bin/python3 /kw2/rank.py [website] [keyword2] 
sleep 20
/usr/bin/python3 /kw3/rank.py [website] [keyword3] 
sleep 30
/usr/bin/python3 /kw4/rank.py [website] [keyword4] 
sleep 25
/usr/bin/python3 /kw5/rank.py [website] [keyword5] 

The problem I am having is that when I run my bash file, all the CSV outputs are stored in the home folder, where the bash file is located and not on the specific folder where the python script.

I tried to add >> and location/output.csv or .txt but the output is in a .txt file or if its in CSV its in one column. Also, this is not my python output, it's only what the terminal outputs when running the python script.

The python code that saves my output to CSV looks like this

file = datetime.date.today().strftime("%d-%m-%Y")+'-' +keyword + '.csv'
with open(file, 'w+') as f:
    writer = csv.writer(f)
    writer.writerow(['Keyword' , 'Rank', 'URL' , 'Date'])
    writer.writerows(zip( d[0::4], d[1::4] , d[2::4], d[3::4]))

I would like to run my bash file on one folder but I want to get my script outputs in the specific folder the python script is located.

Thanks.

Author:Sundios,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/57170626/python-script-output-to-be-saved-in-different-folder
S. Kwak :

Use an absolute path when opening file for writing...\n\nimport os.path\n\n# PRETEND YOU 'example' folder under C:\\\nsave_to_path = 'C://example//'\nname_of_file = input(\"What is the name of the file: \")\ncomplete_name = os.path.join(save_to_path, name_of_file+\".txt\")\n\nwith open(complete_name, 'w+') as f:\n f.write('Hi')\n\n\nNow you have Hi.txt file in C:\\example\\",
2019-07-23T19:20:30
yy