Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    simple file checking

    Scheduled Pinned Locked Moved WiPy 2&3
    5 Posts 3 Posters 3.3k Views 3 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • L Offline
      livius
      last edited by livius

      two questions about simplify things in micropython like it is in normal python

      1. How to check if file exists?
        i do not see possibility to check it simple like os.path.isfile
        maybe i do something wrong but i try "all" simple posibilities and finish in this crap of code:
      len([item for item in os.listdir() if fname==item])>0
      
      1. how to check file size?
        i tied this:
      os.stat(fname).st_size 
      

      but it do not understand st_size
      and now i write exact idx like:

      os.stat(fname)[6]

      think about people first, then computers

      robert-hhR 1 Reply Last reply Reply Quote 0
      • G Offline
        grovalmitch
        last edited by

        Checking for file or directory exists using Python

        import os
        dirname = "temp"
        filename = "my_file"
        #check directory exists
        if(os.path.exists(dirname)):
        print("Directory Exists")
        else:
        print("Directory does not exists")
        #check file exists
        if(os.path.exists(filename)):
        print("File Exists")
        else:
        print("File does not exists")

        Full source...file exist

        1 Reply Last reply Reply Quote 0
        • robert-hhR Offline
          robert-hh @livius
          last edited by

          @livius Hi livius,
          since the path methods are not implemente din MP, you cantry to open the file fro reading, and look on whether you run into an execption of not:

          try:
              f = open(fname, "r")
              exists = True
              f.close()
          except FileNotFoundError:
              exists = False
          

          Ans also, since the symbolic element names for os.stat are not implemented, os.stat[6] is the right value, giving the files size in bytes. Also, a check like: os.stat[0] & 0o170000 == 0o040000 will tell you, that the entry is a directory.

          L 1 Reply Last reply Reply Quote 1
          • L Offline
            livius @robert-hh
            last edited by livius

            Hi @robert-hh :)
            I forgot about this very old question and I solved it myself long ago.
            But this can be helpful for the community :)

            think about people first, then computers

            robert-hhR 1 Reply Last reply Reply Quote 0
            • robert-hhR Offline
              robert-hh @livius
              last edited by

              @livius Sorry, I did n ot look at the date. I was just surprised that you asked.

              1 Reply Last reply Reply Quote 0

              Hello! It looks like you're interested in this conversation, but you don't have an account yet.

              Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

              With your input, this post could be even better 💗

              Register Login
              • 1 / 1
              • First post
                Last post

              Pycom on Twitter

              Pycom