当使用一个传送到另一个的python文件时,我可以将stdin与argv结合使用吗?

时间:2022-08-04 07:27:47

Basically I am running mapreduce so I'm going to pipe in to the reducer. What I'm having trouble with is how to specify a directory path which I will use with os.listdir to essentially get to and then compute all the files in that directory alone. But I know I only want what is in the last directory and not the sub-directories on that same level. I may consider also using fileinput().

基本上我正在运行mapreduce所以我要管道到reducer。我遇到的问题是如何指定一个目录路径,我将使用os.listdir来实际获取然后单独计算该目录中的所有文件。但我知道我只想要最后一个目录中的内容而不是同一级别的子目录。我也可以考虑使用fileinput()。

I think I may be using stdin wrong here, depends on if it has to be from the command line or if it can be indirectly from the command line.

我想我可能在这里使用stdin错误,取决于它是否必须来自命令行,或者它是否可以间接来自命令行。

This was my command-line input: "./path1/path2/path3" ./map.py | sort | ./red.py

这是我的命令行输入:“。/ path1 / path2 / path3”./ map.py |排序| ./red.py

What I got as an error from doing what I want to do is -bash, not a directory ./pythonfile.py

我从做我想做的事情中得到的错误是-bash,而不是目录./pythonfile.py

Using Python 2.7.2

使用Python 2.7.2

This is what I'm doing:

这就是我正在做的事情:

def func():
    path = sys.argv[0]
    return [filenames for filenames in os.listdir(path)if os.path.isfile(os.path.join(path,filenames))]

if func() is not None: 
   for file in func():
       sys.stdin.read()
       ...etc..

1 个解决方案

#1


1  

"./path1/path2/path3" ./map.py | sort | ./red.py

That tells bash to execute the program ./path1/path2/path3 with the argument ./map.py. This is almost certainly not what you want to do.

这告诉bash使用参数./map.py执行程序./path1/path2/path3。这几乎肯定不是你想要做的。

path = sys.argv[0]

This is the name of the script -- the first element of argv[] is, by convention, the name of the program that is being executed. See this:

这是脚本的名称 - 按照惯例,argv []的第一个元素是正在执行的程序的名称。看到这个:

$ cat echo.py 
#!/usr/bin/python
import sys
print sys.argv[0]
$ chmod 755 echo.py
$ ./echo.py
./echo.py
$ 

Perhaps what you intended to do was echo the string to the script:

也许您打算做的是将字符串回显到脚本:

echo path1/path2/path3 | ./map.py | sort | ./red.py

But then you could not use the argv[] array to get at it easily. In fact, it is very difficult to properly parse multiple pathnames from a free-form input like this, so I'd avoid it entirely, and pass the names as arguments:

但是你不能使用argv []数组来轻松实现它。实际上,从这样的*格式输入中正确解析多个路径名是非常困难的,所以我完全避免它,并将名称作为参数传递:

./map.py path1/path2/path3 /a/s/econd/pathname

You could iterate over all paths passed as arguments using something like this:

您可以使用以下内容迭代遍历作为参数传递的所有路径:

>>> a=["nothing", "passwd", "shadow"]
>>> [f for f in a[1:]]
['passwd', 'shadow']
>>> import os
>>> [f for f in a[1:] if os.path.isfile(f)]
['passwd', 'shadow']
>>> 

#1


1  

"./path1/path2/path3" ./map.py | sort | ./red.py

That tells bash to execute the program ./path1/path2/path3 with the argument ./map.py. This is almost certainly not what you want to do.

这告诉bash使用参数./map.py执行程序./path1/path2/path3。这几乎肯定不是你想要做的。

path = sys.argv[0]

This is the name of the script -- the first element of argv[] is, by convention, the name of the program that is being executed. See this:

这是脚本的名称 - 按照惯例,argv []的第一个元素是正在执行的程序的名称。看到这个:

$ cat echo.py 
#!/usr/bin/python
import sys
print sys.argv[0]
$ chmod 755 echo.py
$ ./echo.py
./echo.py
$ 

Perhaps what you intended to do was echo the string to the script:

也许您打算做的是将字符串回显到脚本:

echo path1/path2/path3 | ./map.py | sort | ./red.py

But then you could not use the argv[] array to get at it easily. In fact, it is very difficult to properly parse multiple pathnames from a free-form input like this, so I'd avoid it entirely, and pass the names as arguments:

但是你不能使用argv []数组来轻松实现它。实际上,从这样的*格式输入中正确解析多个路径名是非常困难的,所以我完全避免它,并将名称作为参数传递:

./map.py path1/path2/path3 /a/s/econd/pathname

You could iterate over all paths passed as arguments using something like this:

您可以使用以下内容迭代遍历作为参数传递的所有路径:

>>> a=["nothing", "passwd", "shadow"]
>>> [f for f in a[1:]]
['passwd', 'shadow']
>>> import os
>>> [f for f in a[1:] if os.path.isfile(f)]
['passwd', 'shadow']
>>>