Advertisement
Gulligagardinen

fList (Python)

Aug 22nd, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.62 KB | None | 0 0
  1. import os
  2. import os.path
  3.  
  4. #Check if the file exists
  5. def exist(path):
  6.     if os.path.isfile(path):
  7.         return True, 'file'
  8.     elif os.path.isdir(path):
  9.         return True, 'dir'
  10.     else:
  11.         return False, ''
  12.  
  13. #Returns the file's path or name depended on the rValue("name" or "path")
  14. def fPath(path, rValue):
  15.     exists = exist(path)
  16.     if exists[0] and exists[1] == "file":
  17.         slash = 0
  18.         path = list(path)
  19.         for i in range(len(path)):
  20.             if path[i] == '/' or path[i] == '\\':
  21.                 slash = i
  22.         if rValue == "path":
  23.             return str(''.join(path[0:slash+1]))
  24.         elif rValue == "name":
  25.             return str(''.join(path[slash+1:len(path)]))
  26.     return ''
  27.  
  28. #Get list with all lines from file
  29. def fList(path):
  30.     exists = exist(path)
  31.     if exists[0] and exists[1] == "file":
  32.         f = open(path, 'r')
  33.         _list = f.readlines()
  34.         f.close()
  35.         return _list
  36.     return []
  37.  
  38. #Get specific line from file
  39. def fLine(path, line):
  40.     _list = fList(path)
  41.     if _list[line]:
  42.         return _list[line]
  43.     return ''
  44.  
  45. #Get all text from file
  46. def fText(path):
  47.     exists = exist(path)
  48.     if exists[0] and exists[1] == "file":
  49.         f = open(path, 'r')
  50.         _str = f.read()
  51.         f.close()
  52.         return _str
  53.     return ''
  54.  
  55. #Standard write
  56. def fWrite(path, _str):
  57.     exists = exist(path)
  58.     if exists[0] and exists[1] == "file":
  59.         if list(_str)[len(_str)-1] != '\n':
  60.             _str = _str + '\n'
  61.         f = open(path, 'w')
  62.         f.write(_str)
  63.         f.close()
  64.  
  65. #Standard append
  66. def fAppend(path, _str):
  67.     exists = exist(path)
  68.     if exists[0] and exists[1] == "file":
  69.         if list(_str)[len(_str)-1] != '\n':
  70.             _str = _str + '\n'
  71.         f = open(path, 'a')
  72.         f.write(_str)
  73.         f.close()
  74.  
  75. #Standard write at top
  76. def fTop(path, _str):
  77.     if list(_str)[len(_str) - 1] != '\n':
  78.         _str = _str + '\n'
  79.     fWrite(path, _str + fText(path))
  80.  
  81. #Write from list
  82. def fWriteList(path, _list):
  83.     _str = ''
  84.     for line in _list:
  85.         _str = _str + line
  86.     fWrite(path, _str)
  87.  
  88. #Append from list
  89. def fAppendList(path, _list):
  90.     _str = ''
  91.     for line in _list:
  92.         _str = _str + line
  93.     fAppend(path, _str)
  94.  
  95. #Write at top from list
  96. def fTopList(path, _list):
  97.     _str = ''
  98.     for line in _list:
  99.         _str = _str + line
  100.     fTop(path, _str)
  101.  
  102. #Replacing a line in a file
  103. def fReplaceLine(path, line, _str):
  104.     if list(_str)[len(_str) - 1] != '\n':
  105.         _str = _str + '\n'
  106.     _list = fList(path)
  107.     _list[line-1] = _str
  108.     fWriteList(path, _list)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement