os.path.getsize on Windows reports full file size while copying












2














I am copying a large file, 2.23GB (2,401,129,714 bytes) from one location to another which is on a network share. I am using the below code to check when the file has finished copying by checking the file size. I am on Windows 7 Python 2.7.11 and os.path.getsize reports the full file size as soon as the file starts copying, the file doesn't grow. Is there another way to tell when a file has finished copying within Python?



copying = True
size2 = -1
while copying:
size = os.path.getsize('name of file being copied')
if size == size2:
print "File has finished copying"
break
else:
size2 = os.path.getsize('name of file being copied')
time.sleep(2)









share|improve this question
























  • os.path.getsize reports the full file size as soon as the file starts copying, the file doesn't grow. What does that mean ?
    – Anmol Singh Jaggi
    Jun 5 '16 at 20:47






  • 1




    Possible duplicate of How to check file size in python?
    – Anmol Singh Jaggi
    Jun 5 '16 at 20:51
















2














I am copying a large file, 2.23GB (2,401,129,714 bytes) from one location to another which is on a network share. I am using the below code to check when the file has finished copying by checking the file size. I am on Windows 7 Python 2.7.11 and os.path.getsize reports the full file size as soon as the file starts copying, the file doesn't grow. Is there another way to tell when a file has finished copying within Python?



copying = True
size2 = -1
while copying:
size = os.path.getsize('name of file being copied')
if size == size2:
print "File has finished copying"
break
else:
size2 = os.path.getsize('name of file being copied')
time.sleep(2)









share|improve this question
























  • os.path.getsize reports the full file size as soon as the file starts copying, the file doesn't grow. What does that mean ?
    – Anmol Singh Jaggi
    Jun 5 '16 at 20:47






  • 1




    Possible duplicate of How to check file size in python?
    – Anmol Singh Jaggi
    Jun 5 '16 at 20:51














2












2








2







I am copying a large file, 2.23GB (2,401,129,714 bytes) from one location to another which is on a network share. I am using the below code to check when the file has finished copying by checking the file size. I am on Windows 7 Python 2.7.11 and os.path.getsize reports the full file size as soon as the file starts copying, the file doesn't grow. Is there another way to tell when a file has finished copying within Python?



copying = True
size2 = -1
while copying:
size = os.path.getsize('name of file being copied')
if size == size2:
print "File has finished copying"
break
else:
size2 = os.path.getsize('name of file being copied')
time.sleep(2)









share|improve this question















I am copying a large file, 2.23GB (2,401,129,714 bytes) from one location to another which is on a network share. I am using the below code to check when the file has finished copying by checking the file size. I am on Windows 7 Python 2.7.11 and os.path.getsize reports the full file size as soon as the file starts copying, the file doesn't grow. Is there another way to tell when a file has finished copying within Python?



copying = True
size2 = -1
while copying:
size = os.path.getsize('name of file being copied')
if size == size2:
print "File has finished copying"
break
else:
size2 = os.path.getsize('name of file being copied')
time.sleep(2)






windows file python-2.7 os.path






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jun 5 '16 at 19:03

























asked Jan 4 '16 at 7:48









speedyrazor

1,13231835




1,13231835












  • os.path.getsize reports the full file size as soon as the file starts copying, the file doesn't grow. What does that mean ?
    – Anmol Singh Jaggi
    Jun 5 '16 at 20:47






  • 1




    Possible duplicate of How to check file size in python?
    – Anmol Singh Jaggi
    Jun 5 '16 at 20:51


















  • os.path.getsize reports the full file size as soon as the file starts copying, the file doesn't grow. What does that mean ?
    – Anmol Singh Jaggi
    Jun 5 '16 at 20:47






  • 1




    Possible duplicate of How to check file size in python?
    – Anmol Singh Jaggi
    Jun 5 '16 at 20:51
















os.path.getsize reports the full file size as soon as the file starts copying, the file doesn't grow. What does that mean ?
– Anmol Singh Jaggi
Jun 5 '16 at 20:47




os.path.getsize reports the full file size as soon as the file starts copying, the file doesn't grow. What does that mean ?
– Anmol Singh Jaggi
Jun 5 '16 at 20:47




1




1




Possible duplicate of How to check file size in python?
– Anmol Singh Jaggi
Jun 5 '16 at 20:51




Possible duplicate of How to check file size in python?
– Anmol Singh Jaggi
Jun 5 '16 at 20:51












3 Answers
3






active

oldest

votes


















1














You can use os.stat() as mentioned here.

See this for a great example on usage.






share|improve this answer





















  • os.stat() and os,path.getsize() display final allocated size the only solution is to try to execute your code and have a try catch block, and then sleep-retry
    – amstegraf
    Dec 18 at 18:49





















1














EDIT: Solution-



import os
import time
def main( event, path ):
if os.path.exists(path):
while True:
try:
new_path= path + "_"
os.rename(path,new_path)
os.rename(new_path,path)
time.sleep(0.05)
print("event type: %s path: %s " %(event.event_type, path))
break
except OSError:
time.sleep(0.05)


I utilized the fact that no two processes can simulatenously utilize a file for IO operations. In windows, when a file is copying, it is kept open by an OS process. Once copying is done, the file is closed by the OS process, and the os module in python can finally rename the file successfully



@Anmol- Not a duplicate. The issue with this code , in windows, is that the os reserves a binary file and writes over it while copying, and hence the size displayed by os.stat won't incrementally increase. We would want a piece of code that notifies us whenever image copying has completed. This particular code is a polling code which works well on linux, and notifies us when the copying process is done (in linux, the size increases incrementally with time)



import os
import time
def main( event,path ):
historicalSize = -1
while (historicalSize != os.path.getsize(path)):
historicalSize = os.stat(path).st_size
print("Size now %s" %historicalSize)
time.sleep(1)
else: print( "event type: %s path: %s " %(event.event_type, path))


Output should be like



Size now 124
Size now 12345
Size now 238590
.....


, instead of just



Size now 23459066950





share|improve this answer































    0














    os.stat() and os,path.getsize() display final allocated size the only solution is to try to execute your code and have a try catch block, and then sleep-retry



    This can also be used on Linux, and this way you don't bother to poll the size you just retry. Of course the code below should be improved a little to make sure you don't hit a infinit loop



           while True:
    try:
    OsAbstract.copy(src, file_metas.get_tmp_full_destination())
    break
    except PermissionError:
    time.sleep(0.5)





    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f34586744%2fos-path-getsize-on-windows-reports-full-file-size-while-copying%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      You can use os.stat() as mentioned here.

      See this for a great example on usage.






      share|improve this answer





















      • os.stat() and os,path.getsize() display final allocated size the only solution is to try to execute your code and have a try catch block, and then sleep-retry
        – amstegraf
        Dec 18 at 18:49


















      1














      You can use os.stat() as mentioned here.

      See this for a great example on usage.






      share|improve this answer





















      • os.stat() and os,path.getsize() display final allocated size the only solution is to try to execute your code and have a try catch block, and then sleep-retry
        – amstegraf
        Dec 18 at 18:49
















      1












      1








      1






      You can use os.stat() as mentioned here.

      See this for a great example on usage.






      share|improve this answer












      You can use os.stat() as mentioned here.

      See this for a great example on usage.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Jun 5 '16 at 20:50









      Anmol Singh Jaggi

      3,94711946




      3,94711946












      • os.stat() and os,path.getsize() display final allocated size the only solution is to try to execute your code and have a try catch block, and then sleep-retry
        – amstegraf
        Dec 18 at 18:49




















      • os.stat() and os,path.getsize() display final allocated size the only solution is to try to execute your code and have a try catch block, and then sleep-retry
        – amstegraf
        Dec 18 at 18:49


















      os.stat() and os,path.getsize() display final allocated size the only solution is to try to execute your code and have a try catch block, and then sleep-retry
      – amstegraf
      Dec 18 at 18:49






      os.stat() and os,path.getsize() display final allocated size the only solution is to try to execute your code and have a try catch block, and then sleep-retry
      – amstegraf
      Dec 18 at 18:49















      1














      EDIT: Solution-



      import os
      import time
      def main( event, path ):
      if os.path.exists(path):
      while True:
      try:
      new_path= path + "_"
      os.rename(path,new_path)
      os.rename(new_path,path)
      time.sleep(0.05)
      print("event type: %s path: %s " %(event.event_type, path))
      break
      except OSError:
      time.sleep(0.05)


      I utilized the fact that no two processes can simulatenously utilize a file for IO operations. In windows, when a file is copying, it is kept open by an OS process. Once copying is done, the file is closed by the OS process, and the os module in python can finally rename the file successfully



      @Anmol- Not a duplicate. The issue with this code , in windows, is that the os reserves a binary file and writes over it while copying, and hence the size displayed by os.stat won't incrementally increase. We would want a piece of code that notifies us whenever image copying has completed. This particular code is a polling code which works well on linux, and notifies us when the copying process is done (in linux, the size increases incrementally with time)



      import os
      import time
      def main( event,path ):
      historicalSize = -1
      while (historicalSize != os.path.getsize(path)):
      historicalSize = os.stat(path).st_size
      print("Size now %s" %historicalSize)
      time.sleep(1)
      else: print( "event type: %s path: %s " %(event.event_type, path))


      Output should be like



      Size now 124
      Size now 12345
      Size now 238590
      .....


      , instead of just



      Size now 23459066950





      share|improve this answer




























        1














        EDIT: Solution-



        import os
        import time
        def main( event, path ):
        if os.path.exists(path):
        while True:
        try:
        new_path= path + "_"
        os.rename(path,new_path)
        os.rename(new_path,path)
        time.sleep(0.05)
        print("event type: %s path: %s " %(event.event_type, path))
        break
        except OSError:
        time.sleep(0.05)


        I utilized the fact that no two processes can simulatenously utilize a file for IO operations. In windows, when a file is copying, it is kept open by an OS process. Once copying is done, the file is closed by the OS process, and the os module in python can finally rename the file successfully



        @Anmol- Not a duplicate. The issue with this code , in windows, is that the os reserves a binary file and writes over it while copying, and hence the size displayed by os.stat won't incrementally increase. We would want a piece of code that notifies us whenever image copying has completed. This particular code is a polling code which works well on linux, and notifies us when the copying process is done (in linux, the size increases incrementally with time)



        import os
        import time
        def main( event,path ):
        historicalSize = -1
        while (historicalSize != os.path.getsize(path)):
        historicalSize = os.stat(path).st_size
        print("Size now %s" %historicalSize)
        time.sleep(1)
        else: print( "event type: %s path: %s " %(event.event_type, path))


        Output should be like



        Size now 124
        Size now 12345
        Size now 238590
        .....


        , instead of just



        Size now 23459066950





        share|improve this answer


























          1












          1








          1






          EDIT: Solution-



          import os
          import time
          def main( event, path ):
          if os.path.exists(path):
          while True:
          try:
          new_path= path + "_"
          os.rename(path,new_path)
          os.rename(new_path,path)
          time.sleep(0.05)
          print("event type: %s path: %s " %(event.event_type, path))
          break
          except OSError:
          time.sleep(0.05)


          I utilized the fact that no two processes can simulatenously utilize a file for IO operations. In windows, when a file is copying, it is kept open by an OS process. Once copying is done, the file is closed by the OS process, and the os module in python can finally rename the file successfully



          @Anmol- Not a duplicate. The issue with this code , in windows, is that the os reserves a binary file and writes over it while copying, and hence the size displayed by os.stat won't incrementally increase. We would want a piece of code that notifies us whenever image copying has completed. This particular code is a polling code which works well on linux, and notifies us when the copying process is done (in linux, the size increases incrementally with time)



          import os
          import time
          def main( event,path ):
          historicalSize = -1
          while (historicalSize != os.path.getsize(path)):
          historicalSize = os.stat(path).st_size
          print("Size now %s" %historicalSize)
          time.sleep(1)
          else: print( "event type: %s path: %s " %(event.event_type, path))


          Output should be like



          Size now 124
          Size now 12345
          Size now 238590
          .....


          , instead of just



          Size now 23459066950





          share|improve this answer














          EDIT: Solution-



          import os
          import time
          def main( event, path ):
          if os.path.exists(path):
          while True:
          try:
          new_path= path + "_"
          os.rename(path,new_path)
          os.rename(new_path,path)
          time.sleep(0.05)
          print("event type: %s path: %s " %(event.event_type, path))
          break
          except OSError:
          time.sleep(0.05)


          I utilized the fact that no two processes can simulatenously utilize a file for IO operations. In windows, when a file is copying, it is kept open by an OS process. Once copying is done, the file is closed by the OS process, and the os module in python can finally rename the file successfully



          @Anmol- Not a duplicate. The issue with this code , in windows, is that the os reserves a binary file and writes over it while copying, and hence the size displayed by os.stat won't incrementally increase. We would want a piece of code that notifies us whenever image copying has completed. This particular code is a polling code which works well on linux, and notifies us when the copying process is done (in linux, the size increases incrementally with time)



          import os
          import time
          def main( event,path ):
          historicalSize = -1
          while (historicalSize != os.path.getsize(path)):
          historicalSize = os.stat(path).st_size
          print("Size now %s" %historicalSize)
          time.sleep(1)
          else: print( "event type: %s path: %s " %(event.event_type, path))


          Output should be like



          Size now 124
          Size now 12345
          Size now 238590
          .....


          , instead of just



          Size now 23459066950






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 at 19:55

























          answered Nov 5 at 11:02









          Adithya V

          112




          112























              0














              os.stat() and os,path.getsize() display final allocated size the only solution is to try to execute your code and have a try catch block, and then sleep-retry



              This can also be used on Linux, and this way you don't bother to poll the size you just retry. Of course the code below should be improved a little to make sure you don't hit a infinit loop



                     while True:
              try:
              OsAbstract.copy(src, file_metas.get_tmp_full_destination())
              break
              except PermissionError:
              time.sleep(0.5)





              share|improve this answer


























                0














                os.stat() and os,path.getsize() display final allocated size the only solution is to try to execute your code and have a try catch block, and then sleep-retry



                This can also be used on Linux, and this way you don't bother to poll the size you just retry. Of course the code below should be improved a little to make sure you don't hit a infinit loop



                       while True:
                try:
                OsAbstract.copy(src, file_metas.get_tmp_full_destination())
                break
                except PermissionError:
                time.sleep(0.5)





                share|improve this answer
























                  0












                  0








                  0






                  os.stat() and os,path.getsize() display final allocated size the only solution is to try to execute your code and have a try catch block, and then sleep-retry



                  This can also be used on Linux, and this way you don't bother to poll the size you just retry. Of course the code below should be improved a little to make sure you don't hit a infinit loop



                         while True:
                  try:
                  OsAbstract.copy(src, file_metas.get_tmp_full_destination())
                  break
                  except PermissionError:
                  time.sleep(0.5)





                  share|improve this answer












                  os.stat() and os,path.getsize() display final allocated size the only solution is to try to execute your code and have a try catch block, and then sleep-retry



                  This can also be used on Linux, and this way you don't bother to poll the size you just retry. Of course the code below should be improved a little to make sure you don't hit a infinit loop



                         while True:
                  try:
                  OsAbstract.copy(src, file_metas.get_tmp_full_destination())
                  break
                  except PermissionError:
                  time.sleep(0.5)






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 18 at 18:55









                  amstegraf

                  402311




                  402311






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f34586744%2fos-path-getsize-on-windows-reports-full-file-size-while-copying%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Wiesbaden

                      Marschland

                      Dieringhausen