小编Joe*_*ton的帖子

似乎在shutil.disk_usage()中出现差异

你好StackOverflow人,长期读者,第一次海报.希望我在这里得到所有信息以提出一个有用的问题.

我正在使用shutil.disk_usage()函数来查找特定路径的当前磁盘使用情况(可用,使用的数量等).据我所知,这是os.statvfs()调用的包装器.我发现它没有给出我期望的答案,与Linux中"du"的输出相比.

出于公司隐私原因,我已经模糊了下面的一些路径,但输出和代码在其他方面都没有被删除.我使用的是Python 3.3.2 64位版本.

#!/apps/python/3.3.2_64bit/bin/python3

# test of shutils.diskusage module
import shutil

BytesPerGB = 1024 * 1024 * 1024

(total, used, free) = shutil.disk_usage("/data/foo/")
print ("Total: %.2fGB" % (float(total)/BytesPerGB))
print ("Used:  %.2fGB" % (float(used)/BytesPerGB))

(total1, used1, free1) = shutil.disk_usage("/data/foo/utils/")
print ("Total: %.2fGB" % (float(total1)/BytesPerGB))
print ("Used:  %.2fGB" % (float(used1)/BytesPerGB))
Run Code Online (Sandbox Code Playgroud)

哪个输出:

/data/foo/drivecode/me % disk_usage_test.py
Total: 609.60GB
Used:  291.58GB
Total: 609.60GB
Used:  291.58GB
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,主要问题是我希望"Used"的第二个数量要小得多,因为它是第一个目录的子集.

/data/foo/drivecode/me % du -sh /data/foo/utils
2.0G    /data/foo/utils
Run Code Online (Sandbox Code Playgroud)

尽管我信任"du",但我发现很难相信Python模块也不正确.所以也许只是我对Linux文件系统的理解可能是个问题.:)

我写了一个模块(很大程度上基于SO的某人的代码),它递归地获取了disk_usage,直到现在我才使用它.它看起来与"du"输出相匹配,但是比shutil.disk_usage()函数慢得多,所以我希望我可以让它工作.

非常感谢提前.

python linux shutil

5
推荐指数
2
解决办法
2549
查看次数

Python raw_input搞乱字符串连接

我正在尝试在Python中做一些相对简单的事情,我很惊讶这对于它应该是多么简单而言并不起作用.

我在这里只是想连接三个简单的字符串.输入raw_input的输入在"abc"以下所有情况下:

proj = raw_input("Name of project: ")
print proj
ProjRegex = 'test1' + proj + 'test2'
print ProjRegex
Run Code Online (Sandbox Code Playgroud)

产量:

abc
test2abc
Run Code Online (Sandbox Code Playgroud)

案例2

proj = raw_input("Name of project: ")
print proj
ProjRegex = 'test1%stest2' % (proj)
print ProjRegex
Run Code Online (Sandbox Code Playgroud)

产量:

abc
test2abc
Run Code Online (Sandbox Code Playgroud)

请注意,在两种情况下,不是"test1abctest2"按预期打印,而是将test2替换为test1.

然后我注意到如果我没有使用raw_input,如果我说:

proj = "abc"
ProjRegex = 'test1' + proj + 'test2'
Run Code Online (Sandbox Code Playgroud)

然后它表现得像预期的那样.

那么发生的事情raw_input()是想要进行字符串替换吗?我的理解是它需要键盘输入,剥离换行符,并以字符串形式返回.

python windows string concatenation raw-input

2
推荐指数
1
解决办法
1110
查看次数

标签 统计

python ×2

concatenation ×1

linux ×1

raw-input ×1

shutil ×1

string ×1

windows ×1