1

我编写了一个程序,它遍历目录中的所有文件并查找带有标志的文件,然后将它们输入到不同的程序中。它工作得很好,我现在唯一要做的就是将脚本放在盒子上的一个位置,然后让它知道在我当前所在的目录中查找工作目录。目前我所做的只是将脚本 mv 到我正在工作的任何目录中,然后从那里调用它,但这很乏味,需要我不断地 cp'ing 脚本。

我只是希望有一种更优雅的方式来做到这一点?任何帮助,将不胜感激。

4

4 回答 4

7

关于什么

import os

loc = os.getcwd()
于 2014-01-13T21:55:28.077 回答
1

__file __对象可以返回这样的信息:

import os

os.path.dirname(__file__)
于 2014-01-13T21:55:27.493 回答
0

如果您愿意将工作目录作为参数传递给脚本,则以下方法将起作用。

#!/usr/bin/env python
import sys
import os

workingDir = sys.argv[1]
os.chdir (workingDir)

# Your code here
于 2014-01-13T22:01:16.640 回答
0

听起来像是帮助示例的完美使用os.walk()将是一个很好的起点:

import os
from os.path import join, getsize
for root, dirs, files in os.walk('python/Lib/email'):
    print root, "consumes",
    print sum(getsize(join(root, name)) for name in files),
    print "bytes in", len(files), "non-directory files"
    if 'CVS' in dirs:
        dirs.remove('CVS')  # don't visit CVS directories
于 2014-01-13T22:16:33.043 回答