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
|
|
|
|
# from os import path # path.exists() may used in future.. may be..
|
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):
|
|
|
|
''' truncates the file and create new with contents :param bytes '''
|
|
|
|
with open(self._path, 'r+b') as file:
|
|
|
|
file.write(bytes)
|
|
|
|
|
|
|
|
def read_bytes(self):
|
|
|
|
''' read the file as bytes '''
|
|
|
|
# buf = b''
|
|
|
|
with open(self._path, 'r+b') as file:
|
|
|
|
return file.read() # is this safe?
|
|
|
|
# buf = file.read()
|
|
|
|
# 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
|