我有一个 python 脚本,它导入了一些 Python 模块,例如 bs4
、json
、os
、requests
、信号
、系统
和时间
。有时我会在我的 PWD 中注意到以下文件:
$ ls -la
-rw-rw-r-- 1 dotancohen dotancohen 12429677 Jun 26 11:37 bs4
-rw-rw-r-- 1 dotancohen dotancohen 12291853 Jun 26 11:36 json
-rwxrwxr-x 1 dotancohen dotancohen 1681 Jun 26 11:51 my-app.py
-rw-rw-r-- 1 dotancohen dotancohen 12291851 Jun 26 11:36 os
-rw-rw-r-- 1 dotancohen dotancohen 12291855 Jun 26 11:36 random
-rw-rw-r-- 1 dotancohen dotancohen 12291851 Jun 26 11:36 re
-rw-rw-r-- 1 dotancohen dotancohen 12429682 Jun 26 11:38 requests
-rw-rw-r-- 1 dotancohen dotancohen 7216 Jun 26 11:38 signal
-rw-rw-r-- 1 dotancohen dotancohen 12291852 Jun 26 11:36 sys
-rw-rw-r-- 1 dotancohen dotancohen 12429678 Jun 26 11:36 time
但是,这些不是 Python 文件,而更像是 Postscript 文件。例如:
$ file sys
sys: PostScript document text conforming DSC level 3.0, Level 1
$ head sys
%!PS-Adobe-3.0
%%Creator: (ImageMagick)
%%Title: (sys)
%%CreationDate: (2013-06-26T11:36:13+03:00)
%%BoundingBox: -0 -0 1920 1053
%%HiResBoundingBox: 0 0 1920 1053
%%DocumentData: Clean7Bit
%%LanguageLevel: 1
%%Orientation: Portrait
%%PageOrder: Ascend
我从来没有在这个目录中运行任何与 Postscript 相关的东西。此外,即使我 rm
文件,它们似乎会在一段时间后恢复,所以定期创建它们。鉴于它们与在此目录中的 Python 脚本中导入的 Python 模块同名,我怀疑存在相关性。可能是什么原因?
最佳答案
我会尝试一下...这看起来像是我有时不小心调用脚本而没有指定 Python 解释器时看到的东西,例如我说 ./foo.py
而不是 python foo.py
,或者在脚本开头没有 #!
行。当我这样做时,脚本在一段时间后因语法错误而失败,目录中充满了名为 sys、os、time、et al 的文件——正是我在脚本中导入的模块.这些新文件相当大 (7MB)。执行 head sys
显示的文件内容与您的基本相同。我使用 Linux (Ubuntu) 进行 Python 开发,没有 IDE,只有一个 bash shell。
这是我认为正在发生的事情,至少在 Linux 上是这样:如果你执行 man import
你会看到
import - saves any visible window on an X server and outputs it as an image file. You can capture a single window, the entire screen, or any rectangular portion of the screen.
[...]
The import program is a member of the ImageMagick(1) suite of tools. Use it to capture some or all of an X server screen and save the image to a file.
所以我猜测 shell 正在将 Python import
语句的第一行解释为 /usr/bin/import
命令,并使用“argument” (模块名称)作为要保存的图像文件的名称。因此文件顶部的 PS/ImageMagick 内容。
同样,我只有在偶尔暂停并在没有 Python 解释器的情况下调用我的脚本时才会看到这一点。由于我不知道你的代码或使用条件,我不能保证这是你的确切问题,但我猜你正在做类似的事情(可能是在不知不觉中)。我希望这对您有所帮助,并让您走上正轨。
编辑:这是一个基本上重现您的问题的实验。代码:
import sys
import random
import os
import time
import signal
def main():
sys.stdout.write('foo\n')
if (__name__ == "__main__"):
main()
我在没有 Python 的情况下调用它:
./foo.py
./foo.py: line 8: syntax error near unexpected token `('
./foo.py: line 8: `def main():'
有新文件:
$ ls
total 25096
-rwxr-xr-x 1 doug doug 146 2013-06-27 10:31 foo.py
-rw-r--r-- 1 doug doug 7291759 2013-06-27 10:12 os
-rw-r--r-- 1 doug doug 7291763 2013-06-27 10:12 random
-rw-r--r-- 1 doug doug 1903418 2013-06-27 10:32 signal
-rw-r--r-- 1 doug doug 1903415 2013-06-27 10:32 sys
-rw-r--r-- 1 doug doug 7291761 2013-06-27 10:12 time
看看它们的内容:
$ head sys
%!PS-Adobe-3.0
%%Creator: (ImageMagick)
%%Title: (sys)
%%CreationDate: (2013-06-27T10:32:20-05:00)
%%BoundingBox: 0 0 663 471
%%HiResBoundingBox: 0 0 663 471
%%DocumentData: Clean7Bit
%%LanguageLevel: 1
%%Orientation: Portrait
%%PageOrder: Ascend
再次,我希望这能说明一些问题并有所帮助。
关于python - 为什么在 Python 目录中创建 postscript 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17338265/