1

I made a script that when clicked on it copies all the files of the directory where it was opened in on to a USB. It works inside Pycharm but when I convert it to an executable (When I use pyinstaller to convert the .py to a .exec) it does not work. I’m almost certain I know what’s wrong but I don’t know how to fix it.

import shutil
import os

current = os.getcwd()
list_of_files = os.listdir(current)


def get_files():
    print('CURRENT: ' + current)
    print('File_List: ' + str(list_of_files))

    for files in list_of_files:
        shutil.copy(current + '/' + files, '/Volumes/U/Copy_things')

get_files()

Long story short I’m using os.getcwd() so the file knows where it is located.
When I execute the file in Pycharm the current directory that os.getcwd() gives me is

CURRENT: /Users/MainFrame/Desktop/python_test_hub/move_file_test

But when I open the executable (same folder as the .py file ) and the terminal opens up os.getcwd() gives me is

CURRENT: /Users/MainFrame

So I need to find a way for the executable to open up the terminal where it is located so it can copy those files. I want to be able to execute it from any folder and copy the files to a USB.

Wild Mike
  • 15
  • 3
  • "when I convert it to an executable it does not work" - you have to elaborate a bit more about this step. – yedpodtrzitko Feb 05 '17 at 23:07
  • @yedpodtrzitko When I use pyinstaller to convert the .py to a .exec – Wild Mike Feb 05 '17 at 23:10
  • run .exe it in console/terminal to see errors. – furas Feb 05 '17 at 23:17
  • it doesn't need terminal to copy it. – furas Feb 05 '17 at 23:19
  • @furas When I run the executable the terminal opens up. If I manually go to where the exec is located in terminal and do ./name it works. But I want the terminal to open up to where the executable is located automatically and run the rest of the code when I click it. – Wild Mike Feb 05 '17 at 23:22
  • when you run code then system first open terminal to run this .exe only to display `print()` - but it doesn't need it to copy . Did you run manually this .exe ? – furas Feb 05 '17 at 23:26
  • @furas Yes. I click on the .exec the terminal opens up and it gives me the Current alongs side with the Files. Because it's not in the correct directory I get a ton of files and that causes and error. I know the script works because if I go to the file manually and run it through terminal it works fine. It's a problem with the location that the terminal opens in when I run the .exec – Wild Mike Feb 05 '17 at 23:31
  • not click - open terminal and manually run ./name.exe - maybe it display error message. – furas Feb 05 '17 at 23:32
  • @furas I get no errors. I edited the previous comment to explain more in depth. – Wild Mike Feb 05 '17 at 23:33
  • what directory do you what to copy ? – furas Feb 05 '17 at 23:36
  • @furas it's in the code I want to copy to /Volumes/U/Copy_things from whatever directory I open the .exec in Everything works fine I just need to find a way so when I open the .exec the terminal opens up the the location of the .exec automatically instead of /Users/MainFrame – Wild Mike Feb 05 '17 at 23:41
  • do you need location of file ? Normally it should have full path in `__file__` or in `sys.argv[0]` - see: [Determining application path in a Python EXE generated by pyInstaller](http://stackoverflow.com/questions/404744/determining-application-path-in-a-python-exe-generated-by-pyinstaller) – furas Feb 05 '17 at 23:54

1 Answers1

1

os.getcwd() gets the directory of where the script is executed from, and this isn't necessarily where you're script is located. Pycharm is most likely altering this path when executing the script, as it executes your script from the project path, rather than the python path.

Try os.path.abspath(os.path.dirname(os.sys.argv[0])) instead of os.getcwd().

These answers have more information: os.getcwd() vs os.path.abspath(os.path.dirname(__file__))

Difference between __file__ and sys.argv[0]

Community
  • 1
  • 1
Matts
  • 1,301
  • 11
  • 30
  • I changed os.getcwd() to os.path.abspath(os.path.dirname(__file__)) but it still takes me to /Users/MainFrame when I run the exec – Wild Mike Feb 06 '17 at 00:07
  • Try `os.path.abspath(os.path.dirname(os.sys.argv[0]))` I tested this one and it worked for me. – Matts Feb 06 '17 at 01:00
  • It worked! Thanks, Not sure why I guess I'll have to read up on that more but thank you. – Wild Mike Feb 06 '17 at 01:30