-1

I have a python script that scans some csv files from a directory, gets the last line from each, and adds them all in a new csv file. When running the script inside pycharm it runs correctly and does its designated job, but when trying to run it through a batch file (i need thath to do some automation later on) it returns an empty csv file instead of the one it's supposed to. The batch file is created by writing in a .txt file:

"path of python.exe" "path of the .py file of the script"

and then changing the .txt extension to a .bat one (that's the process i found online about creating batch files) and the script is:

import pandas as pd
import glob
import os
path = r'Path for origin files.'
r_path = r'Path of resulting file'
if os.path.exists(r_path + 'csv'):
   os.remove(r_path + 'csv')
if os.path.exists(r_path + 'txt'):
   os.remove(r_path + 'txt')
files = glob.glob(path)
column_list = [None] * 44
for i in range(44):
    column_list[i] = str(i + 1)
df = pd.DataFrame(columns = column_list)
for name in files:
    df_n = pd.read_csv(name, names = column_list)
    df = df.append(df_n.iloc[-1], ignore_index=True)
    del df_n
df.to_csv(r_path, index=False, header=False)
del df

What am i doing wrong?

Compo
  • 36,585
  • 5
  • 27
  • 39
  • I'm assuming that you've not really used two separate lines in your [tag:batch-file], instead of a single line, `"path of python.exe" "path of the .py file of the script"`. – Compo Jul 29 '19 at 10:38
  • No. I am using only one line, that's how i typed it. The two lines are a result of the site's text editor probably. –  Jul 29 '19 at 10:59
  • If now fixed that, the previous editor had modified your typing unnecessarily. – Compo Jul 29 '19 at 11:31

2 Answers2

1

Pycharm automatically adds an environment variable called PYTHONPATH to the command before it executes it. The PYTHONPATH variable indicates the python process what the base path is for the execution of the script.

For example if your file path is awesomecsv.csv how should the python process know which folder it should look for to find that file?

PYTHONPATH=/my/path/tothefolderwheremyscriptis/ python my_script.py 

above with the PYTHONPATH you tell python what folder you are executing your python command from.

related documentation

matyas
  • 2,696
  • 23
  • 29
  • The PYTHONPATH is the path of my script or the path of my python.exe? –  Jul 29 '19 at 10:56
  • it is the path to the folder where your script runs in. This answer should contain what you need for windows: https://stackoverflow.com/a/4580120/2174832 – matyas Jul 29 '19 at 11:17
  • Excuse me if i seem stupid but, in this post, how do i make the batch file that runs my script after i make the wrapper? –  Jul 29 '19 at 11:30
  • As far as I understood it you need to create a file called pythonpath.bat with the content `@ECHO OFF setlocal set PYTHONPATH=%1 python %2 %3 endlocal` and call your script with this created pythonpath.bat file. `pythonpath.bat my_script.py` (instead of the PYTHONPATH=.... environment variable that you would add in Linux) Unfortunately I cannot test this because I have no Windows environment available – matyas Jul 29 '19 at 11:34
  • I tried what the answer says and it just opens the python.exe which means it does run the pythonpath.bat file only –  Jul 30 '19 at 08:22
  • Now that i read your answer carefully i understood what you are saying. My problem isn't with the PYTHONPATH because i am using the absolute paths. The paths are not "awesomecsv.csv" etc. They are: "C:\Uesrs\myuser\Desktop\awesomecsv.csv" etc. So python already knows where to find the files. It doesn't have to search for them in the python path –  Jul 30 '19 at 09:11
0

Probably the error is in the paths of the csv files, Pycharm probably is setting for you some kind of workspace folder try to use a full path to the directory

  • The scripts are in the .PyCharm 2019CE2019.2 folder if that's what you mean but i am using the absolute paths in the script to be sure –  Jul 29 '19 at 10:39