<p>这可以通过包含</p>
<pre><code>__main__.py
</code></pre>
<p>作为程序入口点的zip存档文件。比如说创建文件.py包含:</p>
^{pr2}$
<p>以及</p>
<pre><code>__main__.py
</code></pre>
<p>包含:</p>
<pre><code>from createfile import *
writefile('test1.txt')
</code></pre>
<p>然后,把它们放进去之后写文件.zip,正在运行</p>
<pre><code>os.system('python writefile.zip')
</code></pre>
<p>导致在当前工作目录中创建test1.txt,并将“hello world”写入其中。在</p>
<p>在IPython中显示:</p>
<pre><code>%ls
Directory of C:\Users\tn\Documents\python\tmp
09/22/2015 11:02 PM <DIR> .
09/22/2015 11:02 PM <DIR> ..
09/22/2015 10:51 PM 52 __main__.py
09/22/2015 10:50 PM 98 createfile.py
09/22/2015 10:51 PM 422 writefile.zip
3 File(s) 572 bytes
2 Dir(s) 304,887,132,160 bytes free
os.system('python writefile.zip')
Out[29]: 0
%ls
Directory of C:\Users\tn\Documents\python\tmp
09/22/2015 11:02 PM <DIR> .
09/22/2015 11:02 PM <DIR> ..
09/22/2015 10:51 PM 52 __main__.py
09/22/2015 10:50 PM 98 createfile.py
09/22/2015 11:02 PM 11 test1.txt
09/22/2015 10:51 PM 422 writefile.zip
4 File(s) 583 bytes
2 Dir(s) 304,887,132,160 bytes free
!type test1.txt # 'type' is the Windows equivilant of the Linux cat command
hello world
!type createfile.py
def writefile(f):
fout = open(f, 'wt')
fout.write('hello world')
fout.close()
!type __main__.py
from createfile import *
writefile('test1.txt')
!7za l writefile.zip
7-Zip (A) 9.20 Copyright (c) 1999-2010 Igor Pavlov 2010-11-18
Listing archive: writefile.zip
Path = writefile.zip
Type = zip
Physical Size = 422
Date Time Attr Size Compressed Name
- -
2015-09-22 22:50:13 ....A 98 76 createfile.py
2015-09-22 22:51:08 ....A 52 52 __main__.py
- -
150 128 2 files, 0 folders
</code></pre>