计算目录中的文件 [英] Counting Files in Directory

查看:46
本文介绍了计算目录中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我一直在这里工作的时间比我想象的要长得多,所以我终于放弃了,这就是为什么我在这里。我用C ++编程,几乎没有任何python ..永远。所以我需要一些帮助,并希望你们都能够以我的方式提出一些知识。


我需要做的是,计算我的python脚本目录中的文件目前正在进行。它只需要能够列出文件,以及可能在该目录中的子目录。


我一直在看这个帖子:< a href =http://bytes.com/topic/python/answers/45188-count-files-directorytarget =_ blank> http://bytes.com/topic/python/answer...iles-目录并且在此页面上尝试了大量解决方案,但仍然会出现错误。我也试图使用os.walk函数,但似乎每次我运行我的脚本它都有效,但它不显示目录中文件数量的任何输出。我甚至使用raw_input()函数来查看是否只是保持脚本打开以查看任何输出,但即便如此......输出是空白的,我所有的都是要退出的raw_input()消息。 br />

所以,任何帮助/解决方案都将非常感谢。非常感谢。

Alright well i''ve been working at this for a ridiculously longer amount of time than i think i should have been, so i finally gave up and that''s why im here. I program in C++ and hardly do any python.. ever. So i need a little help and hope you all will be kind enough to throw some knowledge my way.

What i need to do, is count the files in the directory that my python script is currently in. It needs to be able to list ONLY the files, and NO sub directories that may be in that directory itself.

I''ve been looking at this thread: http://bytes.com/topic/python/answer...iles-directory and have been trying variations of tons of the solutions on this page, but continue to get errors. Im also trying to use the os.walk function, but it seems that everytime i run my script it works, but it does not display any output of the number of files in the directory. I even used the raw_input() function to see if it was just a matter of keeping the script open to see any output, but even so... the output was blank and all i had was the raw_input() message to quit.

So, any help/solutions would be greatly appreciated. Thanks so much.

推荐答案

这比你想象的要容易得多。这两个函数可以为您完成工作: os.listdir() os.path.isfile()
It''s much easier than you might think. These two functions can do the work for you: os.listdir() and os.path.isfile().
展开 | 选择 | Wrap | 行号


好的,我使用了你给我的代码,当我运行脚本时,它可以工作,但仍然没有显示目录中的文件数。当我做python example.py时它执行然后直接移动到命令行。我再次使用raw_input()来查看是否有其他内容,但仍然没有输出。 :(
Ok, i used that code you gave me and when i run the script, it works, but still does not display the number of files in the directory. When i do "python example.py" it executes and then directly moves on to the command line. I used raw_input() again to see if anything else was there, but still no output. :(


Module os 必须导入。
Module os must be imported.
展开 | 选择 | Wrap | 行号


这篇关于计算目录中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆