Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import os.path
- #Check if the file exists
- def exist(path):
- if os.path.isfile(path):
- return True, 'file'
- elif os.path.isdir(path):
- return True, 'dir'
- else:
- return False, ''
- #Returns the file's path or name depended on the rValue("name" or "path")
- def fPath(path, rValue):
- exists = exist(path)
- if exists[0] and exists[1] == "file":
- slash = 0
- path = list(path)
- for i in range(len(path)):
- if path[i] == '/' or path[i] == '\\':
- slash = i
- if rValue == "path":
- return str(''.join(path[0:slash+1]))
- elif rValue == "name":
- return str(''.join(path[slash+1:len(path)]))
- return ''
- #Get list with all lines from file
- def fList(path):
- exists = exist(path)
- if exists[0] and exists[1] == "file":
- f = open(path, 'r')
- _list = f.readlines()
- f.close()
- return _list
- return []
- #Get specific line from file
- def fLine(path, line):
- _list = fList(path)
- if _list[line]:
- return _list[line]
- return ''
- #Get all text from file
- def fText(path):
- exists = exist(path)
- if exists[0] and exists[1] == "file":
- f = open(path, 'r')
- _str = f.read()
- f.close()
- return _str
- return ''
- #Standard write
- def fWrite(path, _str):
- exists = exist(path)
- if exists[0] and exists[1] == "file":
- if list(_str)[len(_str)-1] != '\n':
- _str = _str + '\n'
- f = open(path, 'w')
- f.write(_str)
- f.close()
- #Standard append
- def fAppend(path, _str):
- exists = exist(path)
- if exists[0] and exists[1] == "file":
- if list(_str)[len(_str)-1] != '\n':
- _str = _str + '\n'
- f = open(path, 'a')
- f.write(_str)
- f.close()
- #Standard write at top
- def fTop(path, _str):
- if list(_str)[len(_str) - 1] != '\n':
- _str = _str + '\n'
- fWrite(path, _str + fText(path))
- #Write from list
- def fWriteList(path, _list):
- _str = ''
- for line in _list:
- _str = _str + line
- fWrite(path, _str)
- #Append from list
- def fAppendList(path, _list):
- _str = ''
- for line in _list:
- _str = _str + line
- fAppend(path, _str)
- #Write at top from list
- def fTopList(path, _list):
- _str = ''
- for line in _list:
- _str = _str + line
- fTop(path, _str)
- #Replacing a line in a file
- def fReplaceLine(path, line, _str):
- if list(_str)[len(_str) - 1] != '\n':
- _str = _str + '\n'
- _list = fList(path)
- _list[line-1] = _str
- fWriteList(path, _list)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement