是否有内置函数来获取文件对象的大小(以字节为单位)?我看到有些人这样做:
def getSize(fileobject):
fileobject.seek(0,2) # move the cursor to the end of the file
size = fileobject.tell()
return size
file = open('myfile.bin', 'rb')
print getSize(file)
Run Code Online (Sandbox Code Playgroud)
但根据我使用Python的经验,它有很多辅助函数,所以我猜可能有一个内置函数.
import os, sys
def crawlLocalDirectories(directoryToCrawl):
crawledDirectory = [os.path.join(path, subname) for path, dirnames, filenames in os.walk(directoryToCrawl) for subname in dirnames + filenames]
return crawledDirectory
print crawlLocalDirectories('.')
dictionarySize = {}
def getSizeOfFiles(filesToMeasure):
for everyFile in filesToMeasure:
size = os.path.getsize(everyFile)
dictionarySize[everyFile] = size
return dictionarySize
print getSizeOfFiles(crawlLocalDirectories('.'))
Run Code Online (Sandbox Code Playgroud)
无论何时运行,我得到输出{'example.py':392L},为什么?什么是L?我不想在最后剥掉L.
如果我在没有将其添加到字典的情况下运行它,它将返回文件大小为392.
我想知道是否有可能知道有多少行包含我的文件文本而不使用命令:
with open('test.txt') as f:
text = f.readlines()
size = len(text)
Run Code Online (Sandbox Code Playgroud)
我的文件非常庞大,所以很难使用这种方法......
目前,当我写一个临时:
import tempfile
a = tempfile.TemporaryFile()
a.write(...)
# The only way I know to get the size
a.seek(0)
len(a.read())
Run Code Online (Sandbox Code Playgroud)
有没有更好的办法?
我有一个Python脚本,它读取标记不可读扇区的文件(通常来自光学媒体),允许重新尝试在不同的光学阅读器上读取所述不可读的扇区.
我发现我的脚本不能与块设备(例如/ dev/sr0)一起使用,以便创建包含的ISO9660/UDF文件系统的副本,因为它os.stat().st_size是零.该算法目前需要提前知道文件大小; 我可以改变这一点,但问题(知道块设备大小)仍然存在,而且这里没有回答,所以我打开这个问题.
我知道以下两个相关的SO问题:
因此,我问:在Python中,如何获取块设备文件的文件大小?
我真的害怕这个read()操作,因为它使用内存.例如,任何人都可以通过上传1gb文件DDoS我的服务器,对吗?
name = request.forms.get('name')
data = request.files.get('data')
if name and data.file:
raw = data.file.read() # This is dangerous for big files
filename = data.filename
return "Hello %s! You uploaded %s (%d bytes)." % (name, filename, len(raw))
Run Code Online (Sandbox Code Playgroud)
有没有安全的解决方案来获取上传的文件大小?一个猜测就是从文件系统中获取文件大小; request.files.get('data')可能存储在临时文件中的某个地方吗?
我正在使用大量CSV文件,每个文件包含大量行。我的目标是逐行获取数据,然后使用Python将其写入数据库。但是,因为有大量数据,所以我想跟踪已写入的数据量。为此,我计算了排队的文件数量,并在文件完成时继续添加一个。
我想对CSV文件执行类似的操作,并显示我所在的行以及总共有多少行(例如:)Currently on row 1 of X。我可以很容易地从第一行开始,然后执行以下操作:currentRow += 1,但是我不确定如何通过耗时的阅读行来获得总计。
另外,由于我的CSV文件都存储在zip归档文件中,因此我目前正在使用ZipFile模块读取它们,如下所示:
#The Zip archive and the csv files share the same name
with zipArchive.open(fileName[:-4] + '.csv', 'r') as csvFile:
lines = (line.decode('ascii') for line in csvFile)
currentRow = 1
for row in csv.reader(lines):
print(row)
currentRow += 1
Run Code Online (Sandbox Code Playgroud)
关于如何快速获取CSV文件总行数的任何想法?
我正在使用 subreddit 刮板从墙纸 subreddits 下载图像。我遇到的问题是某些图像的分辨率很小,导致它们在用作墙纸时看起来很糟糕。我发现好看的墙纸所需的最低分辨率是 1920x1080。我现在需要制作一个不断运行的脚本来扫描图像文件夹,查看每个图像分辨率并决定是删除它还是继续下一个图像。我已经在 Python 中修修补补了一个小时左右,但我觉得我无处可去,因为我只是一个初学者并且几个月没有使用 Python。对这个项目的任何帮助都会很棒;)!干杯。
更新:我现在被困在如何让程序运行一个文件夹并查看每张图片。目前我的代码是;
import os
from PIL import Image
while True:
for file in os.listdir(r"C:\\Users\\Barney\\Documents\\sam"):
im = Image.open(file)
x, y = im.size
totalsize = x*y
if totalsize < 2073600:
os.remove(file)
Run Code Online (Sandbox Code Playgroud)
但这会返回错误;
Traceback (most recent call last):
File "C:\Users\Barney\Desktop\imagefilter.py", line 7, in <module>
im = Image.open(file)
File "C:\Python34\lib\site-packages\PIL\Image.py", line 2251, in open
fp = builtins.open(fp, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'Car - 1971 Ford Mustang Mach 1 351 [2560X1600].jpg'
Run Code Online (Sandbox Code Playgroud)
在互联网上我看到我打开程序的地方可能有问题??非常困惑,因为程序正在查看此文件夹并读取内容,因为它说不存在的文件在该文件夹中?有什么帮助吗?
我os.walk用来比较两个文件夹,看看它们是否包含完全相同的文件.但是,这仅检查文件名.我想确保文件大小相同,如果它们有不同的报告.你能从中获得文件大小os.walk吗?
可以说我的结构是这样的
/-- am here
/one/some/dir
/two
/three/has/many/leaves
/hello/world
Run Code Online (Sandbox Code Playgroud)
并说/ one/some/dir包含一个大文件,500mb和/ three/has/many/leaves在每个文件夹中包含一个400mb文件.
我想生成每个目录的大小,以获得此输出
/ - in total for all
/one/some/dir 500mb
/two 0
/three/has/many/leaved - 400mb
/three/has/many 800
/three/has/ 800+someotherbigfilehere
Run Code Online (Sandbox Code Playgroud)
我该怎么做?
在我讨厌之前,我还没有找到回答我问题的链接.我是Python 3的初学者.
我应该编写一个函数来打开我写的文件(data.txt),上面写着'嗨那里!' 使用换行符,假设给我10分.
我在下面编写的代码给了我第一个测试用例值10,但它没有隐藏的测试用例 - 这应该给我一个值81.我的代码有什么问题?
def file_size(lines):
"""docstring"""
with open('data.txt', 'r') as file:
lines = file.read()
return len(lines)
print(file_size('data.txt'))
# data.txt contains 'Hi there!' followed by a new line character.
ans = file_size('alongertextfile.txt')
print(ans)
Run Code Online (Sandbox Code Playgroud) python ×11
filesize ×2
python-3.x ×2
bottle ×1
csv ×1
device ×1
dictionary ×1
directory ×1
file ×1
filesystems ×1
linux ×1
size ×1
text ×1