Last active
July 5, 2020 15:31
-
-
Save NP-chaonay/1234b208845d626f365ae5fa02e7492a to your computer and use it in GitHub Desktop.
Remove last charecter from text-encoded file (UTF-8 is supported).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from types import SimpleNamespace as Namespace | |
import sys | |
# Future function from np_chaonay.main | |
def tmp(err,status_msg=None,IsWarning=False): | |
name=type(err).__name__ | |
if not IsWarning: | |
type_='Error' | |
if status_msg==None: status_msg='Program operation has error due to Python exception.' | |
else: | |
type_='Warning' | |
if status_msg==None: status_msg='Program operation has warning due to Python exception.' | |
print( | |
'['+type_+'] '+status_msg, | |
' From Python exception detailed information:', | |
' Exception Name: '+name, | |
sep='\n') | |
if len(err.args)!=0: | |
print(' Exception arguments:') | |
print(' [Index/Data Table]') | |
print(' '+'0 (General message)'+' : '+str(err.args[0])) | |
for i,j in enumerate(err.args[1:]): print(' '+str(i+1)+' : '+str(j)) | |
else: | |
print(' Exception arguments: No any arguments.') | |
npc_m=Namespace() | |
npc_m.print_err=tmp | |
try: file_path=sys.argv[1] | |
except IndexError: | |
print(sys.argv[0]+': [Error] No file path is inputted.') | |
exit(2) | |
try: | |
try: char_bin=int(sys.argv[2]) | |
except ValueError: | |
print(sys.argv[0]+': [Error] char_bin should be integer in range [1,inf).') | |
exit(2) | |
if char_bin<1: | |
print(sys.argv[0]+': [Error] char_bin should be integer in range [1,inf).') | |
exit(2) | |
except IndexError: char_bin=125000 | |
print( | |
'######## Details ########','\n' | |
'Selected path : ',file_path,'\n', | |
'Charecter bin : ',char_bin,'\n', | |
'\n','Reading...', | |
sep='') | |
with open(file_path) as file_buf: | |
tmp3=0 | |
while True: | |
tmp1=file_buf.read(char_bin) | |
tmp2=len(tmp1) | |
tmp3+=tmp2 | |
if tmp2==char_bin: continue | |
else: break | |
if tmp3==0: | |
print('[WARNING] File has no data.') | |
exit(2) | |
if tmp3>=11: | |
file_buf.seek(tmp3-11) | |
msg=file_buf.read(11) | |
msg_head=repr(msg[0])[1:-1] | |
msg_middle=repr(msg[1:-1])[1:-1] | |
msg_tail=repr(msg[-1])[1:-1] | |
msg_old=msg_middle+msg_tail | |
msg_new=msg_head+msg_middle | |
else: | |
file_buf.seek(0) | |
msg=file_buf.read(tmp3) | |
msg_old=repr(msg)[1:-1] | |
msg_new=repr(msg[:-1])[1:-1] | |
msg_old='Before: '+msg_old | |
msg_new=' After: '+msg_new | |
print( | |
'\n######## Change Confirmation ########','\n', | |
'Last 10 characters comparison (Preview from repr of Python string):','\n',msg_old,'\n',msg_new, | |
sep='') | |
try: input('Confirm? (Enter:Proceed, Ctrl+C:QuitWithoutAnyChange)') | |
except (KeyboardInterrupt,EOFError): print('\nCancelled.'); exit() | |
print('\n######## File Operation ########') | |
try: | |
with open(file_path,'r+') as file_buf: | |
print('Truncating...') | |
file_buf.seek(tmp3-1) | |
file_buf.truncate() | |
print('Completed.') | |
except Exception as err: | |
print('[ERROR] Failed. However most of the situation, file isn\'t changed yet.') | |
npc_m.print_err(err) | |
exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment