3

See update at the end.

I am using Ubuntu Linux 11.10, Python 3.

I wrote a Python script which converts some Qt *.ui files to *.py using pyuic4. Then i want to compile the obtained *.py file to *.pyc and delete the *.py file.

For some reason when i delete a converted *.py file, the *.pyc version is also deleted:

try: command = 'pyuic4 -o /home/vic/ui_form.py /home/vic/form.ui' output = subprocess.check_output(command, shell= True, stderr= subprocess.STDOUT) except subprocess.CalledProcessError as e: print('Failed:', e.output) else: print('Converted %s to %s' % (source, targetName)) # convert *.py to *.pyc and delete the source source = '/home/vic/ui_form.py' target = source + 'c' # py -> pyc py_compile.compile(source, target) #shutil.copy(target, target + '_') # if uncommented - the *.pyc_ file remains os.remove(source) # if commented - both *.py and *.pyc files remain, otherwise both deleted (?) 

I don't know what's happening (see the comments in the code for additional info).

I thought i would have a hint if i find WHO deletes the file - maybe it's pyuic4?

I there a possibility to monitor which process deletes a file?


UPDATE:

I was debugging step by step. After executing os.remove(source) both files (*.py - source, and *.pyc) are deleted.

Could this be some Python behavior?

5
  • source=target - so target is your .py initially? Where does this get initialised? And why do you have target and targetName? Commented Oct 27, 2011 at 10:38
  • sorry i took this from a bigger code. i will simplify this. Commented Oct 27, 2011 at 10:40
  • Watch out copy-paste when coding. A lot of bugs come from copy-pasting code without really focusing on what's being done. Use generic modules instead of copy-pasting. Also this IMHO fits better in StackOverflow. Commented Oct 27, 2011 at 10:59
  • Use strace -f and search for unlink in the log. Commented Oct 27, 2011 at 11:16
  • strace -f helped - it showed the unlink calls, but only *.py files where deleted. could there be a race condition, that compiling *.py -> *.pyc is not finished while *.py is being deleted and *.pyc is also deleted by OS? Commented Oct 27, 2011 at 11:49

4 Answers 4

6

You can set sysctl -w vm.block_dump=1 to see every file system action in dmesg. (high volume, so disable again afterwards)

Sign up to request clarification or add additional context in comments.

Comments

2

I was going crazy all the day with this issue, and, as it often happens, the solution was near, but of a different kind:

I have this project open in Eclipse. When Eclipse is open it tracks the creation of new *.py files (from *.ui or *qrc). Then Eclipse automatically adds them to the project.

When the script converts *.py files to *.pyc and deletes the *.py files - Eclipse also tracks this and carefully deletes the corresponding *.pyc files.

So this is it.

Comments

1

You may use inotify to detect filesystem activity. See the manual. Quoting the manual:

 IN_DELETE File/directory deleted from watched directory (*). IN_DELETE_SELF Watched file/directory was itself deleted. 

Not sure if you can get who deleted it. Another idea:

chattr +i test.py 

and see if some command tries to unsuccessfully delete it (and gives an error message).

Comments

0

You could try using auditd. I haven't tried it, but it might be helpful.

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.