Link to home
Start Free TrialLog in
Avatar of CAE5942
CAE5942

asked on

Modify a small python script

Hi everyone,

I have attached a python script. The code works to merge together a bunch of .csv files and then produces a file called “output.csv”. The problem is that the csv files need to be in the same directory as the python file, whereas I’d like it to run even if the .csv files are in another directory. Is it possible to revise the script so that a directory of my choosing can be hardcoded or even better a directory could be inserted (perhaps via a popup window) before the script runs?

I’m not very familiar with Python so I’d be really grateful if someone could help me modify the script to work as needed.

Thanks in advance.
merge_csv.py
ASKER CERTIFIED SOLUTION
Avatar of Walter Ritzel
Walter Ritzel
Flag of Brazil image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
the line:

cwd = os.getcwd()

Open in new window


can be replaced by:

cwd = "path/to/folder/with/CSV/files"

Open in new window

Avatar of Moussa Mokhtari
Moussa Mokhtari

@gwh2
Change line 67 to
cwd = sys.argv[1]

Open in new window

and line 49 to
 
with open(os.path.join(cwd,'output.csv'), 'wb') as csvfile:

Open in new window


and pass your csv file path like this
ex
python merger_csv.py "path/to/your/csv/files"

Open in new window


this will generate the output.csv file in the same directory as your csv files

Ps : I haven't tested the code yet but this should do it

Cheers
Avatar of CAE5942

ASKER

Sorry for the late response but my subscription expired and I needed time to re-activate it. Thanks for the replies.

#Walter Ritzel
I tried replacing the following line with the path to the csv files:

cwd = os.getcwd()

Open in new window


This worked but the output file is still not being stored in the directory that I chose.

#Moussa Mokhtari
I'll try your suggestion next but you say to "pass your csv file path like this":

 python merger_csv.py "path/to/your/csv/files"

Open in new window


But where do I add the above line in the code? Can you clarify this?
You need to do the changes as @Moussa have suggested.

The line that you are asking to where to put is in fact the command line to call your script. We dont know your environment, but if you are using Windows and just double clicking the script, now you'll need to create a .cmd file with the command stated by Moussa or every time you need to run the script you'll open a command prompt and type as he suggested.
Avatar of CAE5942

ASKER

Thanks for the reply,

I'm creating a .exe file using py2eye because the person who will run the script doesn't have python installed on the computer. Since I'm doing it this way I would need to have the path somewhere in the script itself which is why I was asking where to place it. Since this is the case, can you tell me where to run the script within the python file?

When I first asked the question, you suggested that I put the path here:

cwd = "path/to/folder/with/CSV/files"

Open in new window


I realise that I still need to adjust the code according to what @Moussa has suggested but he he's only said to run the code from the command line but not told me where to add it in the script. Can you help?
No, if this is your case, then it is better to have it in the code, not as a parameter, I would say.
In this case, use my instructions of change and change 3 extra lines as below:
Line 47: def save_csv(keys, results, pathtosave):
Line 49: with open(os.path.join(pathtosave,'output.csv'), 'wb') as csvfile:
Line 75:  save_csv(keys,results, cwd)

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CAE5942

ASKER

Thanks again for the replies,

I'll test it out later on today and come back to you. Much appreciated.
Avatar of CAE5942

ASKER

Thanks again for the information -  I just tested and it works great. When I hardcoded the path I put in the following code:

cwd = "C:\Users\abc\Desktop\mergefiles

Open in new window


This worked correctly because my username is abc and the mergefiles directory is on my desktop. I need to give this to someone though and I only know that they will have the mergefiles directory on their desktop but I don't know their username so is there a way for the path to work correctly without the username so it would look something like this:

cwd = "C:\Users\Desktop\mergefiles

Open in new window


Or would I need to know the exact path for the person using the script?
GWH2,

so what you need to do is this:

    # path from command line 
    cwd = sys.argv[1]

Open in new window


This will make you be able to receive the path via command line. Then, when calling your executable file, you just need to pass it through the command line and it will work.

c:\xxx\xxx\yyyy\someeex.exe c:\a\b\csvfolder

Open in new window

Avatar of CAE5942

ASKER

Thanks for the reply,

I created the .exe file because the person using the script is unable to use the command line, therefore I have to hard-code the path in the python file before creating the .exe file.  Can you confirm that I in fact need to know the exact path and to insert this path in the following line of code:

cwd = "C:\Users\abc\Desktop\mergefiles
Let me check one thing, maybe you can use a system variable.
Here is how to use environment variables:

Create a system environment variable on windows, called CSV_FOLDER and set to your CSV folder;

in your python code:

at the beggining of your code (start at line 4, I presume), add the following, if it is already not present:
import os

Open in new window


Then, change the famous line 67:
cwd = "path/to/csv"

Open in new window


to:
cwd = os.environ["CSV_FOLDER"]

Open in new window


Then, you just need to change it to whatever path you want. You can even do that everytime someone login into the computer.
Avatar of CAE5942

ASKER

Thanks again for the information - I"ll try the environment variables.

Just one final question if that's ok:  as mentioned in an earlier post, I'm using py2exe to create the .exe file on Windows but I wondered is there a similar approach to create an executable file (or similar) for use on a mac? If someone doesn't have python installed on a mac is there a way to run the file without using Terminal or is this only possible in Windows?
I dont think so.  Mac works like Linux, so you need the Python shebang (the line you already have in your script for linux as the first line) and set the chmod to 777 to your script file.
Avatar of CAE5942

ASKER

Ok but what about this:

http://www.pyinstaller.org/

Have you tried it and do you think it would be any good?
I did not have used this tool.