2015-03-21 16:50:38 +01:00
|
|
|
from sys import platform
|
2015-03-17 19:27:20 +01:00
|
|
|
import os
|
2015-03-21 16:50:38 +01:00
|
|
|
from subprocess import call
|
2015-03-21 17:35:00 +01:00
|
|
|
from os.path import exists
|
2015-03-17 19:27:20 +01:00
|
|
|
|
|
|
|
class File():
|
|
|
|
def __init__(self, path):
|
2015-03-21 16:50:38 +01:00
|
|
|
self._path = path
|
|
|
|
|
|
|
|
def write_bytes(self, bytes):
|
2015-03-21 17:35:00 +01:00
|
|
|
''' truncates the file and create new with :param bytes.
|
|
|
|
:return number of bytes written'''
|
|
|
|
with open(self._path, 'w+b') as file:
|
|
|
|
return file.write(bytes)
|
2015-03-21 16:50:38 +01:00
|
|
|
|
|
|
|
def read_bytes(self):
|
2015-03-21 17:35:00 +01:00
|
|
|
''' read the file as bytes. :return b'' on file not exist '''
|
|
|
|
if not exists(self._path): return b''
|
2015-03-21 16:50:38 +01:00
|
|
|
# buf = b''
|
|
|
|
with open(self._path, 'r+b') as file:
|
2015-03-21 17:35:00 +01:00
|
|
|
return file.read()
|
2015-03-21 16:50:38 +01:00
|
|
|
# return buf
|
|
|
|
|
|
|
|
def open(self):
|
|
|
|
'''tries to open with os default viewer'''
|
2015-03-21 17:09:25 +01:00
|
|
|
call(('cmd /c start "" "'+ self._path +'"')if os.name is 'nt' else ('open' if platform.startswith('darwin') else 'xdg-open', self._path))
|
|
|
|
|
|
|
|
def remove(self):
|
|
|
|
''' try to remove the file '''
|
|
|
|
try:
|
|
|
|
os.remove(self._path)
|
|
|
|
except FileNotFoundError: pass
|