0

I'm working on a Python application that renames PDF files based on certain criteria. I use PySide6 for the GUI, and the application is supposed to read PDF files from a directory and rename them.

Here is the snippet that reads the PDF files from a directory:

directory = os.path.dirname(os.path.realpath(__file__))

Option 1

files = [entry.name for entry in os.scandir(self.directory) if entry.is_file() and entry.name.endswith(".pdf")]

Option 2

files = [f for f in os.listdir(self.directory) if os.path.isfile(os.path.join(self.directory, f)) and f.endswith(".pdf")]

Both options work perfectly when I run my script through Python. The renaming part is done through the following code:

if date_str:
    new_file = os.path.join(self.directory, f"{date_str} - {file}")
    os.rename(file_path, new_file)

The issue I'm facing is when I convert my script to an executable using either auto-py-to-exe or Nuitka. The executable launches just fine with both tools and the PySide6 GUI opens up, but it seems to be unable to find any PDF files and doesn't rename anything. There are no errors displayed, it just doesn't perform the expected actions.

python -m nuitka --onefile --follow-imports --windows-disable-console --enable-plugin=pyside6 PDFRenamer.pyw

For context, I'm executing the .exe in the same directory as my .pyw file.

What could be causing this issue? How can I debug or resolve this? Any help or suggestions are highly appreciated.

Python Version 3.11.4

Thank you in advance!

Mike
  • 161
  • 1
  • 12
  • It's likely that turning the script into an executable somehow changes the working directory for the script. Try setting the working directory explicitly using `os.chdir()` – Grismar Jul 06 '23 at 10:06
  • @Grismar I tried directory = os.getcwd() and this seemed to work. Until the virus scanner deleted the file as a supposed trojan :( – Mike Jul 06 '23 at 12:41
  • The behaviour (renaming items on the file system) and nature of the executable (unsigned, containing a lot of surplus code) make it look a lot like a trojan, so that's fair enough. Keep that in mind if you're developing something like this for others, and consider using a more suitable language for compiled executables (and sign the executables). – Grismar Jul 06 '23 at 12:47
  • @Grismar With PyInstaller it worked even without AV alarm. But the file is of course not exactly small with 70 MB. What do you recommend Kotlin, Rust? I appreciate with Python how many libraries there are and how fast you get results. It's more like productivity tools that take work away from individual colleagues for specific tasks, so they can better use a few hours of their time per month. – Mike Jul 06 '23 at 14:40

1 Answers1

0
directory = os.getcwd()

worked also by run the application as a standalone .exe file. With PyInstaller it worked even without AV alarm.

Mike
  • 161
  • 1
  • 12
  • @KJ Yes, I learned and switched to go now. But the pdf libraries aren't very nice for encrypted pdf v4. Often used by banks. I had to made a workaround with Apache Tika to get the text. – Mike Jul 11 '23 at 09:58