#๐Ÿ”’ Try succeeds but except runs anyway.. also unrelated os.path.join() issue...

55 messages ยท Page 1 of 1 (latest)

gilded crystal
#

Here's the function

def cleanup_dir(rootPath):
    pattern_compiled = '*.pyc'
    pattern_decompiled = '*.py'
    for root, subdirs, files in os.walk(rootPath):
        for filename_compiled in fnmatch.filter(files, pattern_compiled):
            fc = str(os.path.join(root, filename_compiled))
            #print(os.path.join(root, os.sep, "ORIGINALS" , filename_compiled))
            #print(f'{root}{os.sep}ORIGINALS{os.sep}{filename}')
            try:
               shutil.move(fc, f'{root}{os.sep}ORIGINALS{os.sep}{filename_compiled}')
               print("Moved %s to .\DECOMPILE\ORIGINALS successfully!" % fc)
            except Exception as ex:
                print("FAILED to move file %s" % fc)
        for filename_decompiled in fnmatch.filter(files, pattern_decompiled):
            fd = str(os.path.join(root, filename_decompiled))
            try:
                shutil.move(fd, f".\Snippets{os.sep}{filename_decompiled}")
                print("Moved %s to .\Snippets successfully!" % fc)
            except Exception as ex:
                print("FAILED to move file %s" %fd)    ``` 

...and here is the output:

Moved .\DECOMPILE\add_to_tuning.pyc to .\DECOMPILE\ORIGINALS successfully!
Moved .\DECOMPILE\detection.pyc to .\DECOMPILE\ORIGINALS successfully!
Moved .\DECOMPILE\injector.pyc to .\DECOMPILE\ORIGINALS successfully!
Moved .\DECOMPILE\snippet.pyc to .\DECOMPILE\ORIGINALS successfully!
Moved .\DECOMPILE\version.pyc to .\DECOMPILE\ORIGINALS successfully!
FAILED to move file .\DECOMPILE\ORIGINALS\add_to_tuning.pyc
FAILED to move file .\DECOMPILE\ORIGINALS\detection.pyc
FAILED to move file .\DECOMPILE\ORIGINALS\injector.pyc
FAILED to move file .\DECOMPILE\ORIGINALS\snippet.pyc
FAILED to move file .\DECOMPILE\ORIGINALS\version.pyc


os.path.join() ignoring first arg "root". commented everything below print():

\ORIGINALS\add_to_tuning.pyc
\ORIGINALS\detection.pyc
\ORIGINALS\injector.pyc
\ORIGINALS\snippet.pyc
\ORIGINALS\version.pyc

zealous cobaltBOT
#

@gilded crystal

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

cedar hedge
#

It's probably the second loop that fails

warm relic
#

remove try except

#

and see what real problem is

gilded crystal
#

same thing, it moved the file successfully, but without except it didnt say it failed to move it

molten cipher
#

you should print out the exception that you're catching.

celest lion
#

it also doesn't look like it says success and failed for the same files

molten cipher
#
 except Exception as ex:
                print(f"FAILED to move file {fd} because {ex}")  
``` e.g.
celest lion
#

@gilded crystal so which file do you suppose it says that it both succeeded and failed?

#

within the same try...except that is?

gilded crystal
#

all the files get moved successfully, its just executing the prints from both try and except

celest lion
#

I disagree โ€“ please answer my question and point out where it ran both the success message and except message within the same loop

gilded crystal
#

i think i was executing from the wrong directory

#

the output that i posted under the code block?

#
Moved .\DECOMPILE\detection.pyc to .\DECOMPILE\ORIGINALS\detection.pyc successfully!
Moved .\DECOMPILE\injector.pyc to .\DECOMPILE\ORIGINALS\injector.pyc successfully!
Moved .\DECOMPILE\snippet.pyc to .\DECOMPILE\ORIGINALS\snippet.pyc successfully!
Moved .\DECOMPILE\TEST.pyc to .\DECOMPILE\ORIGINALS\TEST.pyc successfully!
Moved .\DECOMPILE\version.pyc to .\DECOMPILE\ORIGINALS\version.pyc successfully!
FAILED to move file .\DECOMPILE\ORIGINALS\add_to_tuning.pyc because [Errno 2] No such file or directory: '.\\DECOMPILE\\ORIGINALS\\ORIGINALS\\add_to_tuning.pyc'
FAILED to move file .\DECOMPILE\ORIGINALS\detection.pyc because [Errno 2] No such file or directory: '.\\DECOMPILE\\ORIGINALS\\ORIGINALS\\detection.pyc'
FAILED to move file .\DECOMPILE\ORIGINALS\injector.pyc because [Errno 2] No such file or directory: '.\\DECOMPILE\\ORIGINALS\\ORIGINALS\\injector.pyc'
FAILED to move file .\DECOMPILE\ORIGINALS\snippet.pyc because [Errno 2] No such file or directory: '.\\DECOMPILE\\ORIGINALS\\ORIGINALS\\snippet.pyc'
FAILED to move file .\DECOMPILE\ORIGINALS\TEST.pyc because [Errno 2] No such file or directory: '.\\DECOMPILE\\ORIGINALS\\ORIGINALS\\TEST.pyc'
FAILED to move file .\DECOMPILE\ORIGINALS\version.pyc because [Errno 2] No such file or directory: '.\\DECOMPILE\\ORIGINALS\\ORIGINALS\\version.pyc'```
celest lion
#

you're running two loops, so each file would succeed in the first one and fail in the second one, yes?

gilded crystal
#
def cleanup_dir(rootPath):
    pattern_compiled = '*.pyc'
    pattern_decompiled = '*.py'
    for root, subdirs, files in os.walk(rootPath):
        for filename_compiled in fnmatch.filter(files, pattern_compiled):
            fc = str(os.path.join(root, filename_compiled))
            #print(os.path.join(root, os.sep, "ORIGINALS" , filename_compiled))
            #print(f'{root}{os.sep}ORIGINALS{os.sep}{filename}')
            try:
                shutil.move(fc, f'{root}{os.sep}ORIGINALS{os.sep}{filename_compiled}')
                print(f"Moved {fc} to .\DECOMPILE\ORIGINALS\{filename_compiled} successfully!")
            except Exception as ex:
                print(f"FAILED to move file {fc} because {ex}")```
celest lion
#

what's this? this is only part of the original code

gilded crystal
#

the first loop runs on *.pyc files, the second on *.py files, they are both showing the same result

#

I just dropped the second loop for now because its happening in the first loop too

celest lion
#

and the above output is strictly from a script with ONLY the first loop running at all?

gilded crystal
#

yes

celest lion
#

what's pattern_compiled here?

gilded crystal
#

heres the whole function: ```python
def cleanup_dir(rootPath):
pattern_compiled = '.pyc'
pattern_decompiled = '
.py'
for root, subdirs, files in os.walk(rootPath):
for filename_compiled in fnmatch.filter(files, pattern_compiled):
fc = str(os.path.join(root, filename_compiled))
#print(os.path.join(root, os.sep, "ORIGINALS" , filename_compiled))
#print(f'{root}{os.sep}ORIGINALS{os.sep}{filename}')
try:
shutil.move(fc, f'{root}{os.sep}ORIGINALS{os.sep}{filename_compiled}')
print(f"Moved {fc} to .\DECOMPILE\ORIGINALS{filename_compiled} successfully!")
except Exception as ex:
print(f"FAILED to move file {fc} because {ex}")
# for filename_decompiled in fnmatch.filter(files, pattern_decompiled):
# fd = str(os.path.join(root, filename_decompiled))
# try:
# shutil.move(fd, f".\Snippets{os.sep}{filename_decompiled}")
# print(f"Moved {fd} to .\Snippets{filename_decompiled} successfully!")
# except Exception as ex:
# print(f"FAILED to move file {fd} because {ex}")

cleanup_dir(decompDir)```

#

for fnmatch.filter to find the compiled python files

celest lion
#

right, *.pyc

#

have you tried a debugger?

#

insert breakpoint() before the first for loop, and then use n to step through the code

molten cipher
#

pro tip: use pathlib to construct your paths, not f'{root}{os.sep}ORIGINALS{os.sep}{filename_compiled}'

celest lion
#

that too, yes

gilded crystal
#

i was using os.path.join but it was ignoring the first arg

celest lion
#

um

gilded crystal
#

in the first commented print statement

celest lion
#

that's because the second argument is os.sep

#

so it basically means it starts from the root again

#

file system root that is

#

you don't need to involve os.sep with either pathlib or os.path.join

gilded crystal
#

oh

celest lion
#

I would also like to point out the fact that, if what you claimed was happening and it ran both the try section and the except section, you would get interleaved success and failure messages

#

not a bunch of successes and then all the failures like what you're getting now

#

so I'm guessing that 1) it's finding more .pyc files and the destination directory doesn't exist there

gilded crystal
#

switching the banged up string for os.path.join properly has stopped the messages

celest lion
#

going forward, I suggest you switch to pathlib

#

it's so much easier to work with

gilded crystal
#

Ok

#

Its actually happening again in a different function

celest lion
#

I suggest that you check what exactly it's trying to do, and see if the destination directory actually exists

gilded crystal
#

well it says it failed on one file that it actually succeeded on

celest lion
#

I think you need to rebuild your case for this then

#

but I think we've established that it will not both succeed and fail on the same file

gilded crystal
#

indeed, exeception thrown is something deeper in my decompiler FAILED to decompile .\DECOMPILE\add_to_tuning.pyc because 'SuiteDecompiler' object has no attribute 'BUILD_MAP_UNPACK'

#

Appreciate the assistance, I'll dig into that later

zealous cobaltBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.