3

I'm working on a Python script that goes into a given directory and deletes files based on modification date. I've gotten most of it to work (surprisingly!), except I found a bug. Let me give you all some insight into how my program works.

So, if the user requests that the program runs quietly, I have all of the stdout and stderr information being redirected to a temp text file in a temp folder. Works just fine. There is also an option to run the script continuously. I used a simple while(True) loop to accomplish this. Running continuously worked all fine and dandy, until I wanted to delete that temporary folder after each loop. Here is the code in my main function:

    while(True):

        display()
        verifyDirectory()
        checkArgs()            
        # Deleting the temporary folder after each loop:
        if args.q:
            deleteTempFolder()

Again, everything worked fine until I tried deleting the temp folder after each loop. The program basically stops after the first loop is completed. Here is the code under the deleteTempFolder():

# Closing the redirected text files.
sys.stdout.close()
sys.stderr.close()

# Defining the paths for the temp folder and the existing text files.
folder = args.directory + "\\" + "temp"
stdoutPath = folder + "\\" + "stdout.txt"
stderrPath = folder + "\\" + "stderr.txt"

# Deleting the text files first, then deleting the entire temporary folder.
os.remove(stdoutPath)
os.remove(stderrPath)
os.rmdir(folder)

I should also note that all of the above code works fine on its own, but when I combined the two (run continuously and delete the temporary folder), that's when I noticed that I break out of my loop.

Also, no errors or exceptions are being generated. I was able to check the stderr file by redirecting it to another location, so it wouldn't be deleted when I ran the code.

I hope all of this makes sense. This is the first time I've asked a question on here, and this is the first script I've ever written that wasn't a silly video game in Java for a course. I'm hoping to post the entire script later for advice on efficiency!

10
  • 1
    Do you recreate the folder at the beginning of each loop? Otherwise it's probably failing because you're trying to remove something that doesn't exist. Commented Aug 6, 2015 at 14:53
  • 1
    My primary suspicion is that an exception is being generated, but it's not showing up in your redirected stderr for some reason. Could you delete anything that modifies sys.stdout or sys.stderr, and see what the output is on the console? Commented Aug 6, 2015 at 14:55
  • Wow! You guys are fast! @Dannnno: The folder does get recreated when the checkArgs() function is called. When I don't call deleteTempFolder(), the program runs continuously and just appends to the same file over and over again. If I let the program run for a few minutes, the stdout.txt file gets rather large. So, I thought it would be more efficient to delete after each loop. I'm wondering if I should post the entire script here, but I thought that might be a no-no. Commented Aug 6, 2015 at 15:01
  • @Kevin: Delete anything that modifies them? Do you mean that I should temporarily not redirect them? Perhaps just delete an empty blank folder? Sorry, I'm not following... Commented Aug 6, 2015 at 15:02
  • 1
    @Dannnno : It turns out that you were correct. I was not recreating the folder in the loop. Once I was able to see the errors that were generating, I traced my problem back to that "newbie" error. Thanks so much! Commented Aug 6, 2015 at 18:34

2 Answers 2

2

There is probably an exception being thrown but not displayed because the error stream is closed. Try this code (python boom.py) and comment out the sys.stderr.close() line:

import sys

print("Hello, world")

# this will prevent displaying the exception
sys.stderr.close()

# NameError: name 'nosuchfunction' is not defined
nosuchfunction

print("Goodbye, world")
Sign up to request clarification or add additional context in comments.

2 Comments

Ok, I'm seeing some exceptions now. When I keep the line sys.stdout.close(), I'm getting "I/O operation on closed file". When I comment that line out, I get "The process cannot access the file because it is being used by another process". I noticed that this error happens when I keep the os.remove(stdoutPath) and os.rmdir(folder). Without those three lines, the code works fine... minus the fact that it doesn't actually do what I want lol :)
That was very helpful, thank you! I was able to see the exceptions, and I figured out my problem!
1

I figured out the solution to my problem, thanks to all of the feedback here. It turns out that I my program was generating errors, but I was unable to see until I commented out sys.stderr.close(). Once I could see where and when the program was crashing, I figured out that I was never recreating the temp folder in my loop, which is slightly embarrassing! Thanks for all the feedback!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.