Minor fixes related to io and encodings

master
idziubenko 3 years ago
parent dec15a8ed3
commit 3f52535d86

@ -749,6 +749,9 @@ def pathJoin(*paths):
def listDirectory(directory, fullPath=False, onlyDir=False):
"""Get files from directory, if it exists"""
#just to make sure:
if isinstance(directory, bytes):
directory = directory.decode("UTF-8")
if not path.exists(directory):
return []
try:
@ -837,10 +840,10 @@ def readFile(filename, encoding='utf-8', binary=False):
sys.stderr.write("WARNING: file read error, {}({}:{})\n".format(
str(e), mod, lno))
sys.stderr.flush()
return ""
return "" if not binary else b""
def readFileEx(filename, tailbyte=None, headbyte=None, grab=False, encoding='utf-8'):
def readFileEx(filename, tailbyte=None, headbyte=None, grab=False):
"""
Прочитать целый файл или вернуть пустую строку.
tailbyte: прочитать только последнее указанное количество байт
@ -849,7 +852,7 @@ def readFileEx(filename, tailbyte=None, headbyte=None, grab=False, encoding='utf
"""
try:
if path.exists(filename):
with open(filename, 'rb', encoding=None) as f:
with open(filename, 'rb') as f:
if grab:
filterfunc = lambda s: b"\n".join(
x for x in s.split(b"\n")
@ -869,7 +872,7 @@ def readFileEx(filename, tailbyte=None, headbyte=None, grab=False, encoding='utf
sys.stderr.write("WARNING: file read error, {}({}:{})\n".format(
str(e), mod, lno))
sys.stderr.flush()
return ""
return b""
def writeFile(filename, encoding='utf-8', binary=False):
@ -1530,7 +1533,7 @@ def sha256sum(*fns):
kb = 1024
hasher = sha256()
for fn in fns:
with open(fn, encoding='utf-8') as f:
with open(fn, "rb") as f:
b = True
while b:
b = f.read(512 * kb)

Loading…
Cancel
Save