Python Fu - mkdir not working - Printable Version +- Gimp-Forum.net (https://www.gimp-forum.net) +-- Forum: GIMP (https://www.gimp-forum.net/Forum-GIMP) +--- Forum: Extending the GIMP (https://www.gimp-forum.net/Forum-Extending-the-GIMP) +---- Forum: Scripting questions (https://www.gimp-forum.net/Forum-Scripting-questions) +---- Thread: Python Fu - mkdir not working (/Thread-Python-Fu-mkdir-not-working) |
Python Fu - mkdir not working - BaconWizard17 - 01-27-2023 Hi all, I've got another Python-Fu question. I'm trying to set up a script for exporting a file. As part of that, I want to create a new folder for the output if it doesn't exist. Here's the code: Code: #!/usr/bin/env python Everything here is functional except for the part that creates the new folder. I don't have a function for exporting the image right now. My plan was to work on that after I got the folder creation part figured out. I tested the folderCheck function outside of GIMP in regular Python (with some expected inputs), and it worked completely fine and created the expected folder. Here's that test code for comparison, which worked flawlessly: Code: import os Does GIMP just not have permissions to create a new folder, does this function not work in GIMP, or is there something else going on here? Edit: Something I thought of: Is creating the folder even necessary? Or will GIMP automatically create a folder if it doesn't exist for export? RE: Python Fu - mkdir not working - gasMask - 01-27-2023 Hi, I'm not a Linux user, but here's an article I found on accessing the C Drive on a Linux system. RE: Python Fu - mkdir not working - Kevin - 01-27-2023 At a guess I'd suggest the thing that's breaking is this line: filePathSplit = filePath.split("/") Because pdb.gimp_image_get_filename(image) is going to return a filepath in Windows format because you're runing GIMP on Windows. i.e. C:\Users\ethan\Desktop\Test.xcf You should probably use os.path.dirname(path) to get the directory name: https://docs.python.org/2/library/os.path.html RE: Python Fu - mkdir not working - Ofnuts - 01-27-2023 Also, instead of hard-coding the desktop directory name/location, you can get it as the results of gimp.user_directory(0) RE: Python Fu - mkdir not working - BaconWizard17 - 01-30-2023 (01-27-2023, 07:50 AM)gasMask Wrote: Hi, I use Windows, not Linux (01-27-2023, 09:17 AM)Kevin Wrote: At a guess I'd suggest the thing that's breaking is this line: This generally makes sense. However, when I tested it outside of GIMP in regular Python, it gave me an error when I used the backslash but worked correctly when I used a forward slash, so that's why I coded it that way. Any idea why that might be the case? I'll try using the os function instead though. (01-27-2023, 10:06 AM)Ofnuts Wrote: Also, instead of hard-coding the desktop directory name/location, you can get it as the results of gimp.user_directory(0) I only hardcoded the path when I was testing the code in Python outside of GIMP (to make sure that the non-GIMP syntax worked correctly). In the full GIMP script, I'm using pdb.gimp_image_get_filename(image) to get the file path of the saved file and then manipulating that path to get the output directory. For the sake of documentation, here's the script that I used that now works: Code: def folderCheck(filePath, newFolder): RE: Python Fu - mkdir not working - Kevin - 01-31-2023 (01-30-2023, 04:06 PM)BaconWizard17 Wrote: This generally makes sense. However, when I tested it outside of GIMP in regular Python, it gave me an error when I used the backslash but worked correctly when I used a forward slash, so that's why I coded it that way. Any idea why that might be the case? I'll try using the os function instead though. Because when you wrote it into a string-literal like this: Code: filePath = "C:\Users\ethan\Desktop\Test.xcf" The backslashes are treated as an "escape" sequence and it treats the following character as special if it's a known character, (\n = linefeed, \r = carriage-return, \t = tab etc) so what Python thinks you've assigned filePath to is C:UsersethanDesktopTest.xcf To enter an actual backslash you have to escape it with a backslash! Code: filePath = "C:\\Users\\ethan\\Desktop\\Test.xcf" This translation for special characters only takes place for the string literal, not when doing this: Code: filePath = pdb.gimp_image_get_filename(image) RE: Python Fu - mkdir not working - BaconWizard17 - 02-01-2023 (01-31-2023, 10:12 AM)Kevin Wrote:(01-30-2023, 04:06 PM)BaconWizard17 Wrote: This generally makes sense. However, when I tested it outside of GIMP in regular Python, it gave me an error when I used the backslash but worked correctly when I used a forward slash, so that's why I coded it that way. Any idea why that might be the case? I'll try using the os function instead though. Thanks for clarifying! I remember learning this now. |