Pregunta

I have a python script which imports some Python modules, such as bs4, json, os, requests, signal, sys, and time. Sometimes I notice in my PWD the following files:

$ 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. 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. Furthermore, even if I rm the files they seem to come back after some time, so something in periodically creating them. Seeing as they have the same names as the Python modules being imported in the Python script in this directory, I suspect correlation. What could be the cause?

¿Fue útil?

Solución

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, e.g. I say ./foo.py instead of python foo.py, or do not have a #! 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. These new files are fairly large (7MB). Doing head sys shows file contents essentially the same as yours. I use Linux (Ubuntu) and no IDE for my Python development, just a bash shell.

Here's what I think is going on, at least on Linux: If you do man import you'll see

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.

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. Hence the PS / ImageMagick stuff at the top of the file.

Again, I only see this when I occasionally space out and invoke my script with no Python interpreter. 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:

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top