python operating files

Operating files

Opening file list

Using osmodule

1
2
3
4
5
6
import os

print os.listdir('.') # file list of present directory
print

print os.listdir('../') # file list of parent directory


Recognizing file’s category

  • os.pathmodule recognizes and returns True or False
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    import os
    def filetype(fpath):
    print fpath, ":",
    if os.path.isfile(fpath):
    print 'Regular file'
    if os.path.isdir(fpath):
    print 'Directory'
    if os.path.islink(fpath):
    print 'Symbolic link'

    flist = os.listdir('.')
    for fname in flist:
    filetype(fname)

Permission about file

  1. Learning permission about file

    • os.access(filepath, mode)
      • the values we can put for mode
        • os.F_OK: testing if the file exists
        • os.R_OK: testing about reading permission
        • os.W_OK: testing about writing permission
        • os.X_OK: testing about opening or executing permission
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    import os
    def fileaccess(fpath):
    print fpath, ':',
    if os.access(fpath, os.F_OK):
    print 'Exists',
    else:
    return
    if os.access(fpath, os.R_OK):
    print 'R',
    if os.access(fpath, os.W_OK):
    print 'W',
    if os.access(fpath, os.X_OK):
    print 'X',
    print

    flist = os.listdir('.')
    for fname in flist:
    fileaccess(fname)
  2. Chagning the Permission for the file

    • os.chmod(filepath, mode)
    1
    2
    import os
    os.chmod('sample.txt', 0777)

Manipulating files

  1. Chagning the name of the file

    • os.rename(old_filepath, new_filepath)
    1
    2
    3
    4
    5
    import os
    os.rename('t.txt', 't1.txt')

    print os.access('t.txt', os.F_OK) # checking if the file exists
    print os.access('t1.txt', os.F_OK)
  2. Moving the file

    • os.rename(oldfilepath, new_filepath)

      1
      2
      3
      4
      5
      import os
      os.rename('t.txt', 'example/t1.txt')

      # checking if the file exists
      print os.access('example/t1.txt', os.F_OK)
  3. Copying the file

    • shutilmodule is used
    • shutil.copyfile(src_filepath, dest_filepath)
    1
    2
    3
    4
    import os
    import shutil
    shutil.copyfile('sample.txt', 'sample_new.txt')
    print os.access('sample_new.txt', os.F_OK)

Manipulating the path of the file

  1. Changing relative path to absolute path

    • os.path.abspath - relative path
      • no matter the existance of the file, it changes the path to absolute path
        1
        2
        import os
        print os.path.abspath('o.txt')
  2. Checking if the file exists in the given path

    • os.path.exists(filepath)

Dividing path name

  1. Dividing path and file’s name

    1
    2
    3
    4
    f = '/Users/booski/git/python/t.txt'

    print os.path.basename(f) # extracting only the file's name
    print os.path.dirname(f) # extracting the directory's name
  2. Dividing path and file’s name by one line - basename and dirname are made in a tuple

    1
    print os.path.split(f)
  3. Dividing drive’s name and file path in MS windows

    1
    print os.path.splitdrive(f)
  4. Dividing extension - path and extension are made in a tuple

    1
    print os.path.splitext(f)
Comments