Pages

Showing posts with label file. Show all posts
Showing posts with label file. Show all posts

Tuesday, October 21, 2014

Python: check if file or directory exists








  • os.path.exists to check if file or directory exists.
    import os
    os.path.exists('/tmp/1.txt')
    os.path.exists('/tmp')
    
  • os.path.isfile to check if file exists.
    import os
    os.path.isfile('/tmp/1.txt')
    



Friday, August 29, 2014

Python: read and write binary files








def copy(if_path, of_path):
    with open(if_path, 'rb') as in_file, open(of_path, 'wb') as out_file:
        while True:
            chunk = in_file.read(1024)
            if chunk:
                out_file.write(chunk)
                out_file.flush()
            else:
                break