简体   繁体   English

为什么在Python目录中创建postscript文件?

[英]Why are postscript files being created in Python directory?

I have a python script which imports some Python modules, such as bs4 , json , os , requests , signal , sys , and time . 我有一个python脚本,它导入一些Python模块,如bs4jsonosrequestssignalsystime Sometimes I notice in my PWD the following files: 有时我在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

However, these are not Python files but rather seem to be Postscript files. 但是,这些不是Python文件,而是似乎是Postscript文件。 For instance: 例如:

$ 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

I've never run anything related to Postscript in this directory. 我从未在此目录中运行与Postscript相关的任何内容。 Furthermore, even if I rm the files they seem to come back after some time, so something in periodically creating them. 此外,即使我rm文件,他们似乎来到了一段时间后回来,所以事情在定期创建它们。 Seeing as they have the same names as the Python modules being imported in the Python script in this directory, I suspect correlation. 看到它们与在此目录中的Python脚本中导入的Python模块具有相同的名称,我怀疑它是相关的。 What could be the cause? 可能是什么原因?

I'll take a stab at this... This looks like something I see sometimes when I accidentally invoke a script without specifying a Python interpreter, eg I say ./foo.py instead of python foo.py , or do not have a #! 我会对此进行一次尝试...这看起来像我有时在没有指定Python解释器时调用脚本时看到的东西,例如我说./foo.py而不是python foo.py ,或者没有#! line at the script's beginning. 在脚本的开头。 When I do this, the script fails after some time with a syntax error, and the directory is filled with files named sys, os, time, et al -- exactly the modules I was importing in my script. 当我这样做时,脚本在语法错误一段时间后失败,并且目录中填充了名为sys,os,time 等的文件 - 正是我在脚本中导入的模块。 These new files are fairly large (7MB). 这些新文件相当大(7MB)。 Doing head sys shows file contents essentially the same as yours. 执行head sys显示文件内容与您的文件内容基本相同。 I use Linux (Ubuntu) and no IDE for my Python development, just a bash shell. 我使用Linux(Ubuntu)而没有IDE用于我的Python开发,只是一个bash shell。

Here's what I think is going on, at least on Linux: If you do man import you'll see 这是我认为正在发生的事情,至少在Linux上:如果你man import你会看到

import - saves any visible window on an X server and outputs it as an image file. import - 将任何可见窗口保存在X服务器上并将其作为图像文件输出。 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. 导入程序是ImageMagick(1)工具套件的成员。 Use it to capture some or all of an X server screen and save the image to a file. 使用它来捕获部分或全部X服务器屏幕并将图像保存到文件中。

So I'm guessing that the shell is interpreting the first line of Python import statements as the /usr/bin/import command, and using the "argument" (module name) as the name of the image file to save. 所以我猜测shell将第一行Python import语句解释为/usr/bin/import命令,并使用“argument”(模块名称)作为要保存的图像文件的名称。 Hence the PS / ImageMagick stuff at the top of the file. 因此PS / ImageMagick的东西位于文件的顶部。

Again, I only see this when I occasionally space out and invoke my script with no Python interpreter. 再一次,我只是在偶尔空出并调用没有Python解释器的脚本时才看到这个。 Since I don't know your code or usage conditions I can't guarantee this is your exact issue, but I'm guessing you're doing something similar (maybe unknowingly). 由于我不知道您的代码或使用条件,我无法保证这是您的确切问题,但我猜您正在做类似的事情(可能是在不知不觉中)。 I hope this helps, and gets you on the right track. 我希望这会有所帮助,让你走上正轨。

EDIT: Here's an experiment which basically reproduces your issue. 编辑:这是一个基本上再现您的问题的实验。 Code: 码:

import sys
import random
import os
import time
import signal

def main():
    sys.stdout.write('foo\n')

if (__name__ == "__main__"):
    main()

I invoke it with no Python: 我没有Python调用它:

./foo.py 
./foo.py: line 8: syntax error near unexpected token `('
./foo.py: line 8: `def main():'

New files are there: 有新文件:

$ 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

Look at the contents of them: 看看它们的内容:

$ 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

Again, I hope this sheds some light and helps a bit. 再次,我希望这会有所帮助,并有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 文件未保存在 Python 中的正确目录中 - Files not being saved in the right directory in Python 将python保存到正在运行的py文件的目录中创建的文件夹中 - Make python save to a folder created in the directory of the py file being run 使用 python3 用新创建的文件替换目录中的许多文件 - replacing many files in a directory with newly created files using python3 未在特定目录中创建文件 - File not being created in the specific directory Python看门狗,当修改某些文件时,只创建一个修改过的目录 - Python watchdog, when certain files are modified only a directory modified is created python watchdog监视目录,用于创建文件并对该文件执行操作 - python watchdog monitoring directory for created files and doing something to that file 为什么从字符串模板创建Python的namedtuple实例? - Why Python's namedtuple instance is being created from a string template? 为什么在Python 2.7中无法正确创建我的列表? - Why are my lists not being created properly in Python 2.7? Python无法识别目录 - Directory is not being recognized in Python 为什么我的python datetime对象(从时间戳创建)没有自动创建为UTC? - Why is my python datetime object (created from a timestamp) not automatically being created as UTC?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM