Ras*_*mon 5 python linux django
虽然以下代码在 Windows 中运行良好,但在 Linux 服务器(pythonanywhere)中,该函数仅返回 0,没有错误。我错过了什么?
import os
def folder_size(path):
total = 0
for entry in os.scandir(path):
if entry.is_file():
total += entry.stat().st_size
elif entry.is_dir():
total += folder_size(entry.path)
return total
print(folder_size("/media"))
Run Code Online (Sandbox Code Playgroud)
参考:来自/sf/answers/2615757581/ 的代码
@gilen-tomas 在评论中给出了解决方案:
import os
def folder_size(path):
total = 0
for entry in os.scandir(path):
if entry.is_file():
total += entry.stat().st_size
elif entry.is_dir():
total += folder_size(entry.path)
return total
print(folder_size("/home/your-user/your-proyect/media/"))
Run Code Online (Sandbox Code Playgroud)
需要一个完整的路径!