Linux 无法将输出重定向到ipython ide中的文件

Linux 无法将输出重定向到ipython ide中的文件,linux,ipython,Linux,Ipython,我无法将dir(modulename)的输出重定向到ipython ide中的文件。请告诉我怎么做。它同样适用于ls和其他linux命令 In [45]: os.system('dir(socket) > socket1.txt') File Not Found Out[45]: 1 In [46]: os.system('cat socket1.txt') Volume in drive C is Local Disk Volume Serial Number is D64

我无法将dir(modulename)的输出重定向到ipython ide中的文件。请告诉我怎么做。它同样适用于ls和其他linux命令

 In [45]: os.system('dir(socket) > socket1.txt')
 File Not Found
 Out[45]: 1

 In [46]: os.system('cat socket1.txt')
 Volume in drive C is Local Disk
 Volume Serial Number is D649-B68C

 Directory of C:\Users\aryan\PycharmProjects\classes

 Out[46]: 0

In [47]: dir (socket)
Out[47]:
['AF_APPLETALK',

你在这里混合了两个世界

一个是Python世界,您可以在其中调用Python函数(如
dir()
);在这个世界上,重定向不是简单地使用
或类似的东西来完成的

另一个世界是Shell世界,您可以在其中调用Shell命令行(如
catsocket1.txt
);在这种情况下,可以在命令后使用一个简单的
符号(连同文件名)进行重定向。使用
system()
调用(或来自
子流程
模块的其他调用)进入这个世界

您现在所做的是使用
system()
进入Shell世界,然后在这个世界中执行
dir()
(这不起作用)。不过,重定向将起作用,因此您将创建一个名为
socket1.txt
(可能为空)的文件

重定向Python调用的输出通常不是一件容易的事情。然而,您的情况很特殊:您只想将Python值转储到文件中。这很简单:

with open('socket1.txt', 'w') as out_file:
  out_file.write(str(dir(socket)))
system('grep'timeout\'socket1.txt')也因某些原因无法工作。以下是socket1.txt的截断输出herror、htonl、htons、inet\u aton、inet\u ntoa、inet\u ntop、inet\u pton、io、ntohl、ntohs、os、选择器、setdefaulttimeout、socket、socketpair、sys、timeout']