Search code examples
pythonpython-3.xpython-requestsgauge

How to edit a file to increase the size by adding white spaces at end in python


I want to first create a copy of a file and then check the size of the file and if the size is less than 1 MB then add white spaces at end of the file to make it 1 MB size.

I have copied the using below code but I am getting any help for adding white spaces at end of the file.

from shutil import copyfile
copyfile(self.actualfile,self.copyfile)

Solution

  • you can do it like this:

    import os
    
    filename = 'file.txt'
    
    size = os.stat(filename).st_size
    
    f = open(filename, "a+")
    f.write(" " * (1024*1024 - size))
    f.close();