0

I cannot figure out the best way to find a specific folder and send files to another specific folder, especially if the users directory is slightly different than what I have coded.

I'm working on a program that has a folder of content to grab from and the user basically picks items and when they're done, it creates a folder full of things including the images they chose. I've gotten it to work (and creating all necessary folders in the user's directory works fine but once it becomes more complex, it doesn't work some of the time) but I would like it to work every time, regardless of the user and where they've placed my program on their computer.

an example of relevant code I currently have which I'm sure is redundant compared to what I could be using instead:

init python:
    import os
    import shutil

    current_dir = os.path.normpath(os.getcwd() + "../../")

    def grab_content():
        filetocopy = "image%s.png"%image_choice ##(this is a separate variable within the program that determines if it is image1.png, image2.png etc)
        file_path = os.path.join(current_dir, "Program folder", "stuff", "content")
        images_path = os.path.join(file_path, "images")
        new_images_path = os.path.join(current_dir, "My Templates", anothervariable_name, "game", "template", "image_choices")
        try:
            shutil.copy(images_path + "\\" + filetocopy, new_images_path)
        except:
            print("error")



(folders listed in this have been checked if existing and placed if not - but not for the new file path due to that needing to be in a specific place within the main folder)

It either works if I have the files set up just right (for my own machine which defeats the purpose) or it doesn't do anything or I get an error saying the path doesn't exist. I have code prior to this that creates the folders needed but I'm trying to grab images from the folder that belongs to the actual program and put them (only ones I specify) into a new folder I create through the program.

Would i use os.walk? I was looking at all the os code but this is my first time dealing with any of it so any advice is helpful.

Ali
  • 173
  • 1
  • 3
  • 13
  • 1
    Why are you traversing two directories above the cwd? – clubby789 Aug 06 '19 at 14:12
  • ...yeah...can you be sure that you actually can go up by 2? – mikuszefski Aug 06 '19 at 14:14
  • Because the engine I'm using puts out my folders a certain way and I'm trying to put the folders up further than deep into multiple folders if that makes sense. Otherwise it'll be placing it in game/lib/etc and I want them more accessible where the user can see and grab it. going up two, usually finds "my documents" – Ali Aug 06 '19 at 14:15
  • ...yeah, but you say "regardless where they put the program"....so you cannot be sure...at least an `os.path.exists` check should be done. – mikuszefski Aug 06 '19 at 14:16
  • well my program has folders and subfolders, because of the engine. I didn't want to change too much. It's not even it's own program technically, it's a tool I'm creating that you can use for the engine itself, I'm just not very good at explaining that part I apologize haha and I do check os.path.exists when creating the folders in other functions - it's just this one I have an issue with. I should have included a snippet of that being done. – Ali Aug 06 '19 at 14:18
  • Is there a specific reason why you don't use `os.path.join` in the `shutil.copy()`...the double backslash looks so MS Windows while *game/lib/etc* suggests linux. You are aiming for a platform independent solution I guess. – mikuszefski Aug 06 '19 at 14:22
  • basically the file output doesn't go above the lib folder of this engine because that is where python is stored I guess? so I have to like, dig out of it which is a bit annoying. I could ask the engine creator if I don't find a plausible solution – Ali Aug 06 '19 at 14:24
  • could you try to change the `"\\"` with a proper `os.path.join` – mikuszefski Aug 06 '19 at 14:25
  • platform independent yes plus for some reason it doesn't like me using game/lib/etc, it tells me C:\\me\\documents/game doesn't exist so I changed it all to \\ to match what the program automatically prints as os.path – Ali Aug 06 '19 at 14:25
  • Yeah I could try that, this is looking messy because i was trying multiple things when it wasn't working and I was looking up different ways online and that was one of them. – Ali Aug 06 '19 at 14:26
  • ...ah...don't do that by hand...thats what the `os` package is there for – mikuszefski Aug 06 '19 at 14:26
  • ...backslash and forward slash in one path....tells you something's wrong here. – mikuszefski Aug 06 '19 at 14:28
  • When I'm testing the program without actually building distribution files, I have to use different code altogether to test the file output locations and once the program is built and exported and I use it in my documents for example, I have to be using a completely different code or it won't work. So I figure if I had a code that worked whether I was using the finished program or testing it in another location on my computer, then it should work for anyone (hopefully) – Ali Aug 06 '19 at 14:31
  • (and i think I did the "\\" manually because it wouldn't join paths and file .txt/.png with os.path.join - it wouldn't include the slash itself for some reason) – Ali Aug 06 '19 at 14:32
  • In my experience path construction works very good with `os.path`; so maybe you should play with this a bit more to get it straight. Also check [this](https://stackoverflow.com/a/9856725/803359) for going up in the directories in a platform independent way. – mikuszefski Aug 06 '19 at 14:36
  • I believe I figured it out. It could have been something really small I had off because I started from scratch really slowly and it seems to be working. Thanks everyone – Ali Aug 06 '19 at 15:54
  • Though I do indeed still have to go up in the directory but I even used it on desktop and it worked fine – Ali Aug 06 '19 at 15:55

0 Answers0