【发布时间】:2011-12-16 10:16:06
【问题描述】:
见最后更新。
我使用的是 Ubuntu Linux 11.10、Python 3。
我编写了一个 Python 脚本,它使用 pyuic4 将一些 Qt *.ui 文件转换为 *.py。然后我想把得到的*.py文件编译成*.pyc并删除*.py文件。
由于某种原因,当我删除转换后的 *.py 文件时,*.pyc 版本也被删除:
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 (?)
我不知道发生了什么(有关更多信息,请参阅代码中的 cmets)。
如果我发现 WHO 删除了文件,我想我会有提示 - 也许是 pyuic4?
是否有可能监控哪个进程删除了文件?
更新:
我是一步一步调试的。执行os.remove(source) 后,两个文件(*.py - source 和 *.pyc)都被删除。
这可能是某种 Python 行为吗?
【问题讨论】:
-
source=target- 所以最初的目标是你的 .py 文件?这是在哪里初始化的?为什么你有target和targetName? -
对不起,我从一个更大的代码中得到了这个。我会简化这个。
-
编码时注意复制粘贴。 很多错误来自复制粘贴代码,而没有真正关注正在做什么。使用通用模块而不是复制粘贴。这个恕我直言也更适合 StackOverflow。
-
使用 strace -f 在日志中搜索 unlink。
-
strace -f帮助 - 它显示了unlink调用,但只有*.py文件被删除。是否存在竞争条件,即在 *.py 被删除并且 *.pyc 也被操作系统删除时编译 *.py -> *.pyc 未完成?
标签: linux ubuntu file python process