Jan-25-2017, 08:37 PM
When I tried append() it said I can't do that to a tuple. So not a list. But, I still get a float error when trying enumerate().
I'm trying simplify the code to less lines.
I'm trying simplify the code to less lines.
total = []
for i in xrange(len(filepaths)): filepaths[i] = (os.path.getsize(filepaths[i])) nums = filepaths[i] i = int(math.floor(math.log(nums, 1024))) p = math.pow(1024, i) s = round(nums/p, 2) total = s print total
range(len(something)
,not gone give up on this byte size
so do not call it filepaths.# Bad import os filepaths = ['E:/1py_div/uni.txt', 'E:/1py_div/h.py', 'E:/1py_div/cloud9.txt'] for i in range(len(filepaths)): filepaths[i] = os.path.getsize(filepaths[i]) print(filepaths) # [22, 190, 42043]
# Good import os filepaths = ['E:/1py_div/uni.txt', 'E:/1py_div/h.py', 'E:/1py_div/cloud9.txt'] file_byte_size = [] for index,item in enumerate(filepaths): file_byte_size.append(os.path.getsize(item)) print(file_byte_size) #[22, 190, 42043]Ahh come to think of this,it's easy to forget the the easiest solution.
# Best import os filepaths = ['E:/1py_div/uni.txt', 'E:/1py_div/h.py', 'E:/1py_div/cloud9.txt'] file_byte_size = [] for item in filepaths: file_byte_size.append(os.path.getsize(item)) print(file_byte_size) #[22, 190, 42043]list comprehension is also okay.
import os filepaths = ['E:/1py_div/uni.txt', 'E:/1py_div/h.py', 'E:/1py_div/cloud9.txt'] file_byte_size = [os.path.getsize(item) for item in filepaths] #[22, 190, 42043]