0

I have a script in Python that works fine, it creates about 1000 files once the script is ran.

So I implemented some code to move the files to a new folder once the script is ran.

import glob, os, shutil

source_dir = 'C:/Users/george/Desktop/my aemo app/a'
dst = 'C:/Users/george/Desktop/my aemo app/b'
files = glob.iglob(os.path.join(source_dir, "*.csv"))
for file in files:
    if os.path.isfile(file):
        shutil.copy2(file, dst)

This works fine when I run it in idle, however once made executable and I send the program to someone else, it doesn't move the files for them?

halfer
  • 19,824
  • 17
  • 99
  • 186
user8261831
  • 464
  • 1
  • 4
  • 20
  • Does the other person have a `C:/Users/george` directory on their system? – martineau Jan 23 '19 at 00:48
  • No they do not. – user8261831 Jan 23 '19 at 00:51
  • Is there a way so it doesn't matter? – user8261831 Jan 23 '19 at 00:51
  • If they do not, then the copying won't work. You need to determine the source and destination directory names "on the fly" instead of hardcoding them into the script's code. – martineau Jan 23 '19 at 00:53
  • Right, I was thinking it wouldn't work. How do I go about that then? Thanks btw – user8261831 Jan 23 '19 at 00:54
  • One possibility would be to determine the current user's username. There are ways of doing that on Windows (and most other OSs). Another would be to just ask the user. – martineau Jan 23 '19 at 00:55
  • Is there a way to do that automatically – user8261831 Jan 23 '19 at 00:56
  • It's not automatically. You would probably need to call at least one function. It may also not a great idea hardcoding those sub-directory names in as well—but I have no idea what you're doing overall so can't suggest anything as far as they are concerned. – martineau Jan 23 '19 at 01:01
  • So the program does all this stuff and ends up with about 1000csv files. So when you run the program you end up with like a thousand files wherever you ran the program from. So my goal is to move these thousand files into a new folder so that its not so messy – user8261831 Jan 23 '19 at 01:04
  • Is that kind of thing possible? – user8261831 Jan 23 '19 at 01:04
  • It's possible to move them, but you may need to ask the user where they want them to be put. As far as the current user goes, check-out the question [Is there a portable way to get the current username in Python?](https://stackoverflow.com/questions/842059/is-there-a-portable-way-to-get-the-current-username-in-python)—and not just the first answer. – martineau Jan 23 '19 at 01:07
  • Just wherever the program is, make a folder in the same directory – user8261831 Jan 23 '19 at 01:08
  • `os.getcwd()` returns the current working directory. The script itself has a predefined variable named `__file__` with its filename in it—although using pyinstaller may break that. No more questions, please. – martineau Jan 23 '19 at 01:14
  • A workaround: https://stackoverflow.com/questions/50959340/pyinstaller-exes-file-refers-to-a-py-file – martineau Jan 23 '19 at 01:19

1 Answers1

1

I imagine the problem with sending to another user is that their username probably isn't "george" and therefore their file structure is different than what your program is looking for. I would try using environment variables or relative pathways to move files around on a program that is to be distributed. os documentation: https://docs.python.org/2/library/os.html#process-parameters

relative pathways would be something like ../../file/deepFile/whatYoureLookingFor if you want to go up 2 directories from where you run the program and then dive down another path into file

Reedinationer
  • 5,661
  • 1
  • 12
  • 33
  • You could also have the user specify the path at runtime via GUI creation, or argument parsing. Argument parsing documentation is at https://docs.python.org/2/howto/argparse.html. and GUI creation is typically done with tkinter module for python, a good tutorial is at: http://effbot.org/tkinterbook/ – Reedinationer Jan 23 '19 at 00:49
  • Is there a way so that it doesn't matter where the program is? – user8261831 Jan 23 '19 at 00:49
  • My goal is: If i send someone the program and they open it in their downloads, it runs and makes a new folder in their downloads and puts all their files there. Or if I send If i send someone the program and they open it somewhere else, e.g. their desktop it runs and makes a new folder in their desktop and puts all their files there. – user8261831 Jan 23 '19 at 00:51
  • Is that kind of thing possible? – user8261831 Jan 23 '19 at 00:51
  • Yes, you can use environment variables. Try the following in a console: >import os >os.environ – Reedinationer Jan 23 '19 at 00:52
  • in that case use relative pathways. Don't specify a full path from C:\\...... use something like 'my aemo app/a' instead – Reedinationer Jan 23 '19 at 00:53
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/187149/discussion-between-newtor-and-reedinationer). – user8261831 Jan 23 '19 at 00:53
  • i mean an interactive shell – Reedinationer Jan 23 '19 at 00:53
  • Oh I found the mistake in the code I sent you! where i put `if __name__ == 'main'` it should be: `if __name__ == "__main__"` – Reedinationer Jan 23 '19 at 01:40
  • That created the folders but it doesn’t move them? – user8261831 Jan 23 '19 at 21:48
  • 1
    have you tried troubleshooting with print statements? Something like `for file in files: print(file.__next__())`? – Reedinationer Jan 23 '19 at 22:12