Python os.SEEK_END Examples

The following are 30 code examples of os.SEEK_END(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module os , or try the search function .
Example #1
Source File: autograder.py    From autograder with GNU General Public License v3.0 6 votes vote down vote up
def get_abbrv_string_from_file(self, filename):
        if not os.path.exists(filename):
            return "Can't read from " + filename + " because it doesn't exist."

        with open(filename, 'r', encoding="utf-8", errors='replace') as f:
            if os.path.getsize(filename) > 10000:
                retstring = f.read(4000)
                retstring += "\n\nSNIP SNIP SNIP (leaving out some of the output!)\n\n"
                # f.seek(-4000, os.SEEK_END)
                f.seek(os.path.getsize(filename)-4000)
                retstring += f.read(4000)
            else:
                retstring = f.read()

        return retstring


    # http://stackoverflow.com/questions/800197/ 
Example #2
Source File: tarfile.py    From meddle with MIT License 6 votes vote down vote up
def seek(self, pos, whence=os.SEEK_SET):
        """Seek to a position in the file.
        """
        if self.closed:
            raise ValueError("I/O operation on closed file")

        if whence == os.SEEK_SET:
            self.position = min(max(pos, 0), self.size)
        elif whence == os.SEEK_CUR:
            if pos < 0:
                self.position = max(self.position + pos, 0)
            else:
                self.position = min(self.position + pos, self.size)
        elif whence == os.SEEK_END:
            self.position = max(min(self.size + pos, self.size), 0)
        else:
            raise ValueError("Invalid argument")

        self.buffer = ""
        self.fileobj.seek(self.position) 
Example #3
Source File: tarfile.py    From recruit with Apache License 2.0 6 votes vote down vote up
def seek(self, pos, whence=os.SEEK_SET):
        """Seek to a position in the file.
        """
        if self.closed:
            raise ValueError("I/O operation on closed file")

        if whence == os.SEEK_SET:
            self.position = min(max(pos, 0), self.size)
        elif whence == os.SEEK_CUR:
            if pos < 0:
                self.position = max(self.position + pos, 0)
            else:
                self.position = min(self.position + pos, self.size)
        elif whence == os.SEEK_END:
            self.position = max(min(self.size + pos, self.size), 0)
        else:
            raise ValueError("Invalid argument")

        self.buffer = b""
        self.fileobj.seek(self.position) 
Example #4
Source File: petition.py    From petitions with MIT License 6 votes vote down vote up
def get_latest_saved_article_id() -> int:
    """이미 저장한 가장 최근 글번호를 가져오기. 저장된 글이 없으면 0을 반환"""
    # 글이 없으면 0
    if not os.path.isfile(CSV_WHOLE):
        return 0

    # 파일 끝 부분에서 몇 줄 읽어온 뒤 마지막 줄의 첫 칼럼(article_id) 반환
    with open(CSV_WHOLE, 'rb') as f:
        # 마지막 줄을 빠르게 찾기 위해 "거의" 끝 부분으로 이동
        f.seek(0, os.SEEK_END)
        f.seek(-min([f.tell(), 1024 * 100]), os.SEEK_CUR)

        # 마지막 줄에서 article id 추출
        last_line = f.readlines()[-1].decode('utf-8')
        article_id = int(last_line.split(',')[0])

        return article_id 
Example #5
Source File: archive.py    From bob with GNU General Public License v3.0 6 votes vote down vote up
def __putUploadFile(self, url, tmp):
        # Determine file length outself and add a "Content-Length" header. This
        # used to work in Python 3.5 automatically but was removed later.
        tmp.seek(0, os.SEEK_END)
        length = str(tmp.tell())
        tmp.seek(0)
        connection = self._getConnection()
        connection.request("PUT", url, tmp, headers={ 'Content-Length' : length,
            'If-None-Match' : '*', 'User-Agent' : 'BobBuildTool/{}'.format(BOB_VERSION) })
        response = connection.getresponse()
        response.read()
        if response.status == 412:
            # precondition failed -> lost race with other upload
            raise ArtifactExistsError()
        elif response.status not in [200, 201, 204]:
            raise ArtifactUploadError("PUT {} {}".format(response.status, response.reason)) 
Example #6
Source File: tarfile.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def seek(self, pos, whence=os.SEEK_SET):
        """Seek to a position in the file.
        """
        if self.closed:
            raise ValueError("I/O operation on closed file")

        if whence == os.SEEK_SET:
            self.position = min(max(pos, 0), self.size)
        elif whence == os.SEEK_CUR:
            if pos < 0:
                self.position = max(self.position + pos, 0)
            else:
                self.position = min(self.position + pos, self.size)
        elif whence == os.SEEK_END:
            self.position = max(min(self.size + pos, self.size), 0)
        else:
            raise ValueError("Invalid argument")

        self.buffer = ""
        self.fileobj.seek(self.position) 
Example #7
Source File: tarfile.py    From Python24 with MIT License 6 votes vote down vote up
def seek(self, pos, whence=os.SEEK_SET):
        """Seek to a position in the file.
        """
        if self.closed:
            raise ValueError("I/O operation on closed file")

        if whence == os.SEEK_SET:
            self.position = min(max(pos, 0), self.size)
        elif whence == os.SEEK_CUR:
            if pos < 0:
                self.position = max(self.position + pos, 0)
            else:
                self.position = min(self.position + pos, self.size)
        elif whence == os.SEEK_END:
            self.position = max(min(self.size + pos, self.size), 0)
        else:
            raise ValueError("Invalid argument")

        self.buffer = b""
        self.fileobj.seek(self.position) 
Example #8
Source File: meta_data.py    From kobo-predict with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def create_media(media):
    """Download media link"""
    if is_valid_url(media.data_value):
        filename = media.data_value.split('/')[-1]
        data_file = NamedTemporaryFile()
        content_type = mimetypes.guess_type(filename)
        with closing(requests.get(media.data_value, stream=True)) as r:
            for chunk in r.iter_content(chunk_size=CHUNK_SIZE):
                if chunk:
                    data_file.write(chunk)
        data_file.seek(os.SEEK_SET, os.SEEK_END)
        size = os.path.getsize(data_file.name)
        data_file.seek(os.SEEK_SET)
        media.data_value = filename
        media.data_file = InMemoryUploadedFile(
            data_file, 'data_file', filename, content_type,
            size, charset=None)

        return media

    return None 
Example #9
Source File: tarfile.py    From kobo-predict with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def seek(self, pos, whence=os.SEEK_SET):
        """Seek to a position in the file.
        """
        if self.closed:
            raise ValueError("I/O operation on closed file")

        if whence == os.SEEK_SET:
            self.position = min(max(pos, 0), self.size)
        elif whence == os.SEEK_CUR:
            if pos < 0:
                self.position = max(self.position + pos, 0)
            else:
                self.position = min(self.position + pos, self.size)
        elif whence == os.SEEK_END:
            self.position = max(min(self.size + pos, self.size), 0)
        else:
            raise ValueError("Invalid argument")

        self.buffer = b""
        self.fileobj.seek(self.position) 
Example #10
Source File: erpnext_sync.py    From biometric-attendance-sync-tool with GNU General Public License v3.0 6 votes vote down vote up
def get_last_line_from_file(file):
    # concerns to address(may be much later):
        # how will last line lookup work with log rotation when a new file is created?
            #- will that new file be empty at any time? or will it have a partial line from the previous file?
    line = None
    if os.stat(file).st_size < 5000:
        # quick hack to handle files with one line
        with open(file, 'r') as f:
            for line in f:
                pass
    else:
        # optimized for large log files
        with open(file, 'rb') as f:
            f.seek(-2, os.SEEK_END)
            while f.read(1) != b'\n':
                f.seek(-2, os.SEEK_CUR)
            line = f.readline().decode()
    return line 
Example #11
Source File: common.py    From deplicate with MIT License 6 votes vote down vote up
def sidesum(filename, chksize, bufsize, offset=0):
    if bufsize < chksize:
        bufsizes = (bufsize, chksize % bufsize)
    else:
        bufsizes = (chksize, 0)

    offset = abs(offset)

    with readopen(filename, sequential=False, direct=True) as (read, fd):
        whence = (offset, os.SEEK_SET)
        header = _chunksum(fd, read, chksize, bufsizes, whence)

        whence = (-chksize - offset, os.SEEK_END)
        footer = _chunksum(fd, read, chksize, bufsizes, whence)

    return header, footer 
Example #12
Source File: logcat.py    From mobly with Apache License 2.0 6 votes vote down vote up
def _open_logcat_file(self):
        """Create a file object that points to the beginning of the logcat file.
        Wait for the logcat file to be created by the subprocess if it doesn't
        exist.
        """
        if not self._adb_logcat_file_obj:
            start_time = time.time()
            while not os.path.exists(self.adb_logcat_file_path):
                if time.time() > start_time + CREATE_LOGCAT_FILE_TIMEOUT_SEC:
                    raise Error(
                        self._ad,
                        'Timeout while waiting for logcat file to be created.')
                time.sleep(1)
            self._adb_logcat_file_obj = io.open(
                self.adb_logcat_file_path, 'r', encoding='utf-8',
                errors='replace')
            self._adb_logcat_file_obj.seek(0, os.SEEK_END) 
Example #13
Source File: utils.py    From clusterfuzz with Apache License 2.0 6 votes vote down vote up
def read_from_handle_truncated(file_handle, max_len):
  """Read from file handle, limiting output to |max_len| by removing output in
  the middle."""
  file_handle.seek(0, os.SEEK_END)
  file_size = file_handle.tell()
  file_handle.seek(0, os.SEEK_SET)

  if file_size <= max_len:
    return file_handle.read()

  # Read first and last |half_max_len| bytes.
  half_max_len = max_len // 2
  start = file_handle.read(half_max_len)
  file_handle.seek(file_size - half_max_len, os.SEEK_SET)
  end = file_handle.read(half_max_len)

  truncated_marker = b'\n...truncated %d bytes...\n' % (file_size - max_len)

  return start + truncated_marker + end 
Example #14
Source File: tarfile.py    From FuYiSpider with Apache License 2.0 6 votes vote down vote up
def seek(self, pos, whence=os.SEEK_SET):
        """Seek to a position in the file.
        """
        if self.closed:
            raise ValueError("I/O operation on closed file")

        if whence == os.SEEK_SET:
            self.position = min(max(pos, 0), self.size)
        elif whence == os.SEEK_CUR:
            if pos < 0:
                self.position = max(self.position + pos, 0)
            else:
                self.position = min(self.position + pos, self.size)
        elif whence == os.SEEK_END:
            self.position = max(min(self.size + pos, self.size), 0)
        else:
            raise ValueError("Invalid argument")

        self.buffer = b""
        self.fileobj.seek(self.position) 
Example #15
Source File: tarfile.py    From FuYiSpider with Apache License 2.0 6 votes vote down vote up
def seek(self, pos, whence=os.SEEK_SET):
        """Seek to a position in the file.
        """
        if self.closed:
            raise ValueError("I/O operation on closed file")

        if whence == os.SEEK_SET:
            self.position = min(max(pos, 0), self.size)
        elif whence == os.SEEK_CUR:
            if pos < 0:
                self.position = max(self.position + pos, 0)
            else:
                self.position = min(self.position + pos, self.size)
        elif whence == os.SEEK_END:
            self.position = max(min(self.size + pos, self.size), 0)
        else:
            raise ValueError("Invalid argument")

        self.buffer = b""
        self.fileobj.seek(self.position) 
Example #16
Source File: tarfile.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def seek(self, pos, whence=os.SEEK_SET):
        """Seek to a position in the file.
        """
        if self.closed:
            raise ValueError("I/O operation on closed file")

        if whence == os.SEEK_SET:
            self.position = min(max(pos, 0), self.size)
        elif whence == os.SEEK_CUR:
            if pos < 0:
                self.position = max(self.position + pos, 0)
            else:
                self.position = min(self.position + pos, self.size)
        elif whence == os.SEEK_END:
            self.position = max(min(self.size + pos, self.size), 0)
        else:
            raise ValueError("Invalid argument")

        self.buffer = b""
        self.fileobj.seek(self.position) 
Example #17
Source File: plist.py    From USBMap with MIT License 6 votes vote down vote up
def parse(self, fp):
        try:
            # The basic file format:
            # HEADER
            # object...
            # refid->offset...
            # TRAILER
            self._fp = fp
            self._fp.seek(-32, os.SEEK_END)
            trailer = self._fp.read(32)
            if len(trailer) != 32:
                raise InvalidFileException()
            (
                offset_size, self._ref_size, num_objects, top_object,
                offset_table_offset
            ) = struct.unpack('>6xBBQQQ', trailer)
            self._fp.seek(offset_table_offset)
            self._object_offsets = self._read_ints(num_objects, offset_size)
            self._objects = [_undefined] * num_objects
            return self._read_object(top_object)

        except (OSError, IndexError, struct.error, OverflowError,
                UnicodeDecodeError):
            raise InvalidFileException() 
Example #18
Source File: tarfile.py    From BinderFilter with MIT License 6 votes vote down vote up
def seek(self, pos, whence=os.SEEK_SET):
        """Seek to a position in the file.
        """
        if self.closed:
            raise ValueError("I/O operation on closed file")

        if whence == os.SEEK_SET:
            self.position = min(max(pos, 0), self.size)
        elif whence == os.SEEK_CUR:
            if pos < 0:
                self.position = max(self.position + pos, 0)
            else:
                self.position = min(self.position + pos, self.size)
        elif whence == os.SEEK_END:
            self.position = max(min(self.size + pos, self.size), 0)
        else:
            raise ValueError("Invalid argument")

        self.buffer = ""
        self.fileobj.seek(self.position) 
Example #19
Source File: vss.py    From Python-Digital-Forensics-Cookbook with MIT License 6 votes vote down vote up
def __init__(self, file_path, offset=0, sector_size=512):
    """Provide a file like object of a volume inside a disk image.
    Args:
      file_path: String, denoting the file path to the disk image.
      offset: An offset in bytes to the volume within the disk.
      sector_size: The size in bytes of a single sector, defaults to 512.
    """
    self._block_size = 0
    self._offset_start = 0
    self._orig_offset = offset

    ofs = int(offset / sector_size)
    self._block_size, self._image_size = GetImageSize(file_path, ofs)

    self._fh = open(file_path, 'rb')
    self._fh.seek(0, os.SEEK_END)
    self._fh_size = self._fh.tell()
    self._image_offset = ofs

    if self._block_size:
      self._offset_start = self._image_offset * self._block_size
      self._fh.seek(self._offset_start, 0) 
Example #20
Source File: tarfile.py    From Computable with MIT License 6 votes vote down vote up
def seek(self, pos, whence=os.SEEK_SET):
        """Seek to a position in the file.
        """
        if self.closed:
            raise ValueError("I/O operation on closed file")

        if whence == os.SEEK_SET:
            self.position = min(max(pos, 0), self.size)
        elif whence == os.SEEK_CUR:
            if pos < 0:
                self.position = max(self.position + pos, 0)
            else:
                self.position = min(self.position + pos, self.size)
        elif whence == os.SEEK_END:
            self.position = max(min(self.size + pos, self.size), 0)
        else:
            raise ValueError("Invalid argument")

        self.buffer = ""
        self.fileobj.seek(self.position) 
Example #21
Source File: clog.py    From fermentrack with MIT License 6 votes vote down vote up
def tail(f, lines=1, _buffer=4098):
    """Tail a file and get X lines from the end"""
    lines_found = []
    # block counter will be multiplied by buffer
    # to get the block size from the end
    block_counter = -1
    # loop until we find X lines
    while len(lines_found) < lines:
        try:
            f.seek(block_counter * _buffer, os.SEEK_END)
        except IOError:  # either file is too small, or too many lines requested
            f.seek(0)
            lines_found = f.readlines()
            break
        lines_found = f.readlines()
        # we found enough lines, get out
        if len(lines_found) > lines:
            break
        # decrement the block counter to get the
        # next X bytes
        block_counter -= 1
    return lines_found[-lines:] 
Example #22
Source File: tarfile.py    From oss-ftp with MIT License 6 votes vote down vote up
def seek(self, pos, whence=os.SEEK_SET):
        """Seek to a position in the file.
        """
        if self.closed:
            raise ValueError("I/O operation on closed file")

        if whence == os.SEEK_SET:
            self.position = min(max(pos, 0), self.size)
        elif whence == os.SEEK_CUR:
            if pos < 0:
                self.position = max(self.position + pos, 0)
            else:
                self.position = min(self.position + pos, self.size)
        elif whence == os.SEEK_END:
            self.position = max(min(self.size + pos, self.size), 0)
        else:
            raise ValueError("Invalid argument")

        self.buffer = b""
        self.fileobj.seek(self.position) 
Example #23
Source File: tarfile.py    From oss-ftp with MIT License 6 votes vote down vote up
def seek(self, pos, whence=os.SEEK_SET):
        """Seek to a position in the file.
        """
        if self.closed:
            raise ValueError("I/O operation on closed file")

        if whence == os.SEEK_SET:
            self.position = min(max(pos, 0), self.size)
        elif whence == os.SEEK_CUR:
            if pos < 0:
                self.position = max(self.position + pos, 0)
            else:
                self.position = min(self.position + pos, self.size)
        elif whence == os.SEEK_END:
            self.position = max(min(self.size + pos, self.size), 0)
        else:
            raise ValueError("Invalid argument")

        self.buffer = ""
        self.fileobj.seek(self.position) 
Example #24
Source File: tarfile.py    From Mastering-Elasticsearch-7.0 with MIT License 6 votes vote down vote up
def seek(self, pos, whence=os.SEEK_SET):
        """Seek to a position in the file.
        """
        if self.closed:
            raise ValueError("I/O operation on closed file")

        if whence == os.SEEK_SET:
            self.position = min(max(pos, 0), self.size)
        elif whence == os.SEEK_CUR:
            if pos < 0:
                self.position = max(self.position + pos, 0)
            else:
                self.position = min(self.position + pos, self.size)
        elif whence == os.SEEK_END:
            self.position = max(min(self.size + pos, self.size), 0)
        else:
            raise ValueError("Invalid argument")

        self.buffer = b""
        self.fileobj.seek(self.position) 
Example #25
Source File: tarfile.py    From Mastering-Elasticsearch-7.0 with MIT License 6 votes vote down vote up
def seek(self, pos, whence=os.SEEK_SET):
        """Seek to a position in the file.
        """
        if self.closed:
            raise ValueError("I/O operation on closed file")

        if whence == os.SEEK_SET:
            self.position = min(max(pos, 0), self.size)
        elif whence == os.SEEK_CUR:
            if pos < 0:
                self.position = max(self.position + pos, 0)
            else:
                self.position = min(self.position + pos, self.size)
        elif whence == os.SEEK_END:
            self.position = max(min(self.size + pos, self.size), 0)
        else:
            raise ValueError("Invalid argument")

        self.buffer = b""
        self.fileobj.seek(self.position) 
Example #26
Source File: ccl_bplist.py    From iTunes_Backup_Reader with MIT License 6 votes vote down vote up
def load(f):
    """
    Reads and converts a file-like object containing a binary property list.
    Takes a file-like object (must support reading and seeking) as an argument
    Returns a data structure representing the data in the property list
    """
    # Check magic number
    if f.read(8) != b"bplist00":
        raise BplistError("Bad file header")

    # Read trailer
    f.seek(-32, os.SEEK_END)
    trailer = f.read(32)
    offset_int_size, collection_offset_size, object_count, top_level_object_index, offest_table_offset = struct.unpack(">6xbbQQQ", trailer)

    # Read offset table
    f.seek(offest_table_offset)
    offset_table = []
    for i in range(object_count):
        offset_table.append(__decode_multibyte_int(f.read(offset_int_size), False))
    
    return __decode_object(f, offset_table[top_level_object_index], collection_offset_size, offset_table) 
Example #27
Source File: tarfile.py    From anpr with Creative Commons Attribution 4.0 International 6 votes vote down vote up
def seek(self, pos, whence=os.SEEK_SET):
        """Seek to a position in the file.
        """
        if self.closed:
            raise ValueError("I/O operation on closed file")

        if whence == os.SEEK_SET:
            self.position = min(max(pos, 0), self.size)
        elif whence == os.SEEK_CUR:
            if pos < 0:
                self.position = max(self.position + pos, 0)
            else:
                self.position = min(self.position + pos, self.size)
        elif whence == os.SEEK_END:
            self.position = max(min(self.size + pos, self.size), 0)
        else:
            raise ValueError("Invalid argument")

        self.buffer = b""
        self.fileobj.seek(self.position) 
Example #28
Source File: tarfile.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def seek(self, pos, whence=os.SEEK_SET):
        """Seek to a position in the file.
        """
        if self.closed:
            raise ValueError("I/O operation on closed file")

        if whence == os.SEEK_SET:
            self.position = min(max(pos, 0), self.size)
        elif whence == os.SEEK_CUR:
            if pos < 0:
                self.position = max(self.position + pos, 0)
            else:
                self.position = min(self.position + pos, self.size)
        elif whence == os.SEEK_END:
            self.position = max(min(self.size + pos, self.size), 0)
        else:
            raise ValueError("Invalid argument")

        self.buffer = b""
        self.fileobj.seek(self.position) 
Example #29
Source File: client.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def _determineLength(self, fObj):
        """
        Determine how many bytes can be read out of C{fObj} (assuming it is not
        modified from this point on).  If the determination cannot be made,
        return C{UNKNOWN_LENGTH}.
        """
        try:
            seek = fObj.seek
            tell = fObj.tell
        except AttributeError:
            return UNKNOWN_LENGTH
        originalPosition = tell()
        seek(0, os.SEEK_END)
        end = tell()
        seek(originalPosition, os.SEEK_SET)
        return end - originalPosition 
Example #30
Source File: pygtide.py    From pygtide with Mozilla Public License 2.0 6 votes vote down vote up
def read_etpolut1_dat(self):
        fname = os.path.join(ETPRED_DIR, self.data_dir, 'etpolut1.dat')
        with open(fname, "rb") as f:
            first = f.readline()
            #print(first[0:10])
            while first[0:10] != b"C*********":
                first = f.readline()
                #print(first[0:10])
            first = f.readline()
            # Jump to the second last byte.
            f.seek(-12, os.SEEK_END)
            # Until EOL is found...
            while f.read(1) != b"\n":
                # ...jump back the read byte plus one more.
                f.seek(-2, os.SEEK_CUR)
            last = f.readline()
        # store dates
        self.etpolut1_start = dt.datetime.strptime(first[0:8].decode("utf-8"), "%Y%m%d")
        self.etpolut1_end = dt.datetime.strptime(last[0:8].decode("utf-8"), "%Y%m%d")