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?
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.