abstract file class with multi platform opening method

This commit is contained in:
chidea 2015-03-22 00:50:38 +09:00
parent 79ea16f1dd
commit 911e2dad2d

View File

@ -1,7 +1,25 @@
from sys import platform
import os
from subprocess import call
# from os import path # path.exists() may used in future.. may be..
class File():
def __init__(self, path):
# os.open(path,
pass
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'''
call(('cmd /c start "" "'+ self._path +'"')if os.name is 'nt' else ('open' if platform.startswith('darwin') else 'xdg-open', self._path))