python 中的 os.walk 未使用作为路径传递的命令行参数运行

发布于 2024-09-02 13:01:52 字数 855 浏览 1 评论 0原文

我需要查找系统上文件夹中的文件数量。

这就是我使用的:

file_count = sum((len(f) for _, _, f in os.walk('path')))

当我们将路径指定为引号中的字符串时,效果很好,但是当我输入保存路径的变量名时, type(file_count) 是一个生成器对象,因此不能用作整数。

如何解决这个问题以及为什么会发生这种情况?

好的,这就是我正在做的:

在终端的命令行中:

python mypyProg.py arg1 arg2 arg3

在 myProg.py 中:

arg1 = sys.argv[1]
file_count = sum((len(f) for _, _, f in os.walk(arg1)))

arg1 作为字符串传递

我检查了 repr(arg1) 和 type(arg1):

repr(arg1) '/home/kartik/Downloads/yahoo_dataset/tryfolder'
type(arg1) <type 'str'>

type(file_count) <type 'generator'>

错误消息:

NDCG = scipy.zeros((file_count,1),float)

    TypeError: an integer is required

我不知道,它当我仅使用一些虚拟变量进入 IDLE python IDE 时,它运行良好。

I needed to find the number of files in a folder on the system.

This is what i used:

file_count = sum((len(f) for _, _, f in os.walk('path')))

This works fine when we specify the path as a string in quotes, but when I enter a variable name that holds the path, type(file_count) is a generator object, and hence cannot be used as an integer.

How to solve this and why does this happen?

Ok, here's what i'm doing:

in the command line at the terminal:

python mypyProg.py arg1 arg2 arg3

in myProg.py:

arg1 = sys.argv[1]
file_count = sum((len(f) for _, _, f in os.walk(arg1)))

arg1 is passed as a string

I checked repr(arg1) and type(arg1):

repr(arg1) '/home/kartik/Downloads/yahoo_dataset/tryfolder'
type(arg1) <type 'str'>

type(file_count) <type 'generator'>

Error message:

NDCG = scipy.zeros((file_count,1),float)

    TypeError: an integer is required

I don't know, it is running fine in the IDLE python IDE when i enter it using just some dummy variables.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

墨离汐 2024-09-09 13:01:52

我假设您使用 walk 是因为您想了解目录及其子目录中的每个文件。我不明白这里发生了什么:

file_count = sum((len(f) for _, _, f in os.walk(path)))

假设路径包含“src”,它是我的主目录中的一个目录我得到了目录及其后代中的文件数量,那么你是什么意思?您确定从命令行正确读取了路径吗?可以多发帖吗?

I assume you're using walk because you want to know every single file in the directory and its subdirectories. I do not understand what happens here:

file_count = sum((len(f) for _, _, f in os.walk(path)))

Assuming path contains, let's say, 'src' which is a directory in my home dir I get the number of files in the dir and its descendants, so what do you mean? Are you sure you're reading correctly the path from the command line? Can you post more?

狼性发作 2024-09-09 13:01:52

sum 应该返回一个整数,就像在我的 python shell 中一样......

>>> x = '/tmp'
>>> file_count = sum((len(f) for _, _, f in os.walk(x)))
>>> file_count
11
>>> type(file_count)
<type 'int'>

sum should return an integer, as it does in my python shell, too ...

>>> x = '/tmp'
>>> file_count = sum((len(f) for _, _, f in os.walk(x)))
>>> file_count
11
>>> type(file_count)
<type 'int'>
拍不死你 2024-09-09 13:01:52

由于我在该目录中只有文件,因此我使用了这个:

file_count = len([f for f in os.listdir(loadpathTest) if os.path.isfile(os.path.join(loadpathTest, f))])

这似乎有效。

@全部
感谢您的帮助。

Since I had only files in that directory, i used this instead:

file_count = len([f for f in os.listdir(loadpathTest) if os.path.isfile(os.path.join(loadpathTest, f))])

This seems to work.

@All
Thanks for your help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文