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. 如何从单一路径获取上层路径?
  2. Python NameError:全局名称“Form”没有定义pyqt
  3. Python(名称空间、函数嵌套、函数对象)
  4. Gunicorn + Django + Heroku。Python的路径问题
  5. py2exe使用相对路径的当前目录问题
  6. tensorflow 变量定义路径//问题
  7. python在windows中的文件路径问题
  8. Python添加默认模块搜索包路径
  9. 如何获得正在运行的Python脚本的路径?(复制)

随机推荐

  1. Android(安卓)JNI 实例
  2. Android面面观——Android事件处理下(按键
  3. android 自定义ListView中的单项选择
  4. 2011.07.18(2)——— android Animation的
  5. Android中安装,启动和卸载应用
  6. Android USB debugging 功能失效
  7. Android开发7:单元测试
  8. 仿UC底部菜单栏实例源码
  9. 在Android上创建一个PopupWindow
  10. Android Support Package 支撑包