#IMPORT SOME MODULES
import os #OPEN FILE
import sys #GET PARAM FROM CONSOLE AND USE TO EXIT IF ERROR RAISE
import re #REGULAR EKSPRESION
#HARDCODE PARAM IS HERE
#PATH TO SEARCH BIG FILE
PATH = r'C:\\Users\\Borko\\Desktop\\split_files\\Errors.log'
#PATH TO FOLDER WHERE PUT RESULT AND FROM WHERE CREATE FILE BACK
PATH2 = r'C:\\Users\\Borko\\Desktop\\'
#CHUNK SIZE TO READ FILE 1Mb
CHUNK_SIZE = (1024*1024)
#NAME TO SMALL FILES
some_name = 'some_name'
#Check Path exists close program if not
try:
open(PATH, 'r')
except FileNotFoundError as fr :
print(fr)
sys.exit()
#OPEND FILE USE CHUNK AND ITER RESULT WITH YIELD
def read_file(PATH):
lines = open(PATH, 'br').read()
while lines:
chunk = lines[:CHUNK_SIZE]
lines = lines[CHUNK_SIZE:]
yield chunk
#WRITE SMALL CHUNK TO NEW SMALL FILES WITH NOT EXTENSION
#EXTENSION FOR SMALL FILES COME FROM PARAM count
def write_to_files(PATH2):
count = 1
for res in read_file(PATH):
name = '{}{}'.format(some_name, count)
w = open(os.path.join(PATH2, name), 'bw')
w.write(res)
w.close()
count += 1
#READ FOLDER PATH2 AND SEARCH FOR FILES'S THEN RETURN LIST WITH THEM
def concat_chunk_file_to_one():
found_chink_files = []
for file in os.listdir(PATH2):
if re.search('({})\d+'.format(some_name), file):
found_chink_files.append(os.path.join(PATH2, file))
return sorted(found_chink_files)
#CONCAT ALL FOUND SMALL FILES TO ONE BIG
def write_concat_to_one_big():
w = open(PATH2+'\\'+'BIG_FILE.txt', 'a')
for line in concat_chunk_file_to_one():
w.write(open(line, 'r').read())
#print(line)
w.close()
if __name__ == '__main__':
#UNCOMENT TO SPLIT FILE TO SMALL
#write_to_files(PATH2)
#UNCOMENT TO JOIN SMALL FILES TO ONE BIG
#write_concat_to_one_big()
1.01.2018 г.
30.12.2017 г.
Calculate size of folder with python scripts only folder not RAR
#Import module
import os
import sys
class InFolder:
def __init__(self, folder):
self.folder = folder
def search(self):
founded = set()
for root, dirname, filename in os.walk(self.folder):
clean_root = os.path.normpath(root)
clear_root_name = os.path.normcase(clean_root)
founded.add(clear_root_name)
founded.remove(self.folder)
return founded
if len(sys.argv) == 1:
print("Try again with add path to scan!")
sys.exit('Usage: python %s path-name' % sys.argv[0])
elif not os.path.exists(sys.argv[1]):
print("Try again with add path to scan!")
sys.exit('Usage: python %s and corect file name (example c:\\)' % sys.argv[0])
sys.exit()
else:
my_project = InFolder(sys.argv[1])
clear_folder = my_project.search()
class Second:
def __init__(self, path2):
self.path2 = path2
def calculate_size(self):
my_calc = dict()
size = 0
for root, dirname, filename in os.walk(self.path2):
for file in filename:
f_path = os.path.join(root, file)
size += os.path.getsize(f_path)
my_calc[self.path2] = (size/1024)/1024
return my_calc
total = dict()
for f in clear_folder:
main = Second(f)
for i, v in main.calculate_size().items():
total[i] = v
for line in sorted(total.items(), key=lambda x: x[1]):
print('{0:>10} - {1:.2f}Mb'.format(*line))
import os
import sys
class InFolder:
def __init__(self, folder):
self.folder = folder
def search(self):
founded = set()
for root, dirname, filename in os.walk(self.folder):
clean_root = os.path.normpath(root)
clear_root_name = os.path.normcase(clean_root)
founded.add(clear_root_name)
founded.remove(self.folder)
return founded
if len(sys.argv) == 1:
print("Try again with add path to scan!")
sys.exit('Usage: python %s path-name' % sys.argv[0])
elif not os.path.exists(sys.argv[1]):
print("Try again with add path to scan!")
sys.exit('Usage: python %s and corect file name (example c:\\)' % sys.argv[0])
sys.exit()
else:
my_project = InFolder(sys.argv[1])
clear_folder = my_project.search()
class Second:
def __init__(self, path2):
self.path2 = path2
def calculate_size(self):
my_calc = dict()
size = 0
for root, dirname, filename in os.walk(self.path2):
for file in filename:
f_path = os.path.join(root, file)
size += os.path.getsize(f_path)
my_calc[self.path2] = (size/1024)/1024
return my_calc
total = dict()
for f in clear_folder:
main = Second(f)
for i, v in main.calculate_size().items():
total[i] = v
for line in sorted(total.items(), key=lambda x: x[1]):
print('{0:>10} - {1:.2f}Mb'.format(*line))
16.12.2017 г.
Read excell file with python, validate and return defaultdict
import os
import xlrd
from collections import defaultdict
import sys
from pprint import pprint
import re
'''
Some name1 2345 someemal1@gmail.com
Some name12 23456 someemal2@gmail.com
Some name1 0 someemal1@gmail.com
Some name3 0889345 someemal1@gmail.com
'''
filename = "somefile.xls"
result = defaultdict(list)
def checkFilename(filename):
'''Check file is excel or not '''
if filename.endswith('xls'):
return filename
else:
return 'Not valid filename'
sys.exit()
class ReadXLS:
def __init__(self, checkFilename):
self.filename = checkFilename
def realReadFilename(self):
'''Read xls file with xlrd module '''
try:
workbook = xlrd.open_workbook(self.filename)
except:
print("some error...")
sys.exit()
else:
try:
sheet = workbook.sheet_by_index(0)
except IndexError as ie:
print(ie)
sys.exit()
else:
for rowx in range(sheet.nrows):
columnmapping = sheet.row_values(rowx)
yield columnmapping
def checkResult(self):
'''add to result only what i whant'''
for line in ReadXLS.realReadFilename(self):
if ((line[0] in (None, '')) or (line[1] in (None, ''))
or (line[2] in (None, '')) or not
re.search(r'[\w.-]+@[\w.-]+.\w+', line[2])):
continue
else:
#result[line[2].upper()].append((line[0], line[1]))
result[line[2].upper()].append(
(line[1].strip().replace(' ', ''), line[0].strip())
)
def __repr__(self):
''' Why I can '''
return "{}".format(self.filename)
if __name__ == '__main__':
test = ReadXLS(checkFilename(filename))
test.checkResult()
pprint(result)
import xlrd
from collections import defaultdict
import sys
from pprint import pprint
import re
'''
Some name1 2345 someemal1@gmail.com
Some name12 23456 someemal2@gmail.com
Some name1 0 someemal1@gmail.com
Some name3 0889345 someemal1@gmail.com
'''
filename = "somefile.xls"
result = defaultdict(list)
def checkFilename(filename):
'''Check file is excel or not '''
if filename.endswith('xls'):
return filename
else:
return 'Not valid filename'
sys.exit()
class ReadXLS:
def __init__(self, checkFilename):
self.filename = checkFilename
def realReadFilename(self):
'''Read xls file with xlrd module '''
try:
workbook = xlrd.open_workbook(self.filename)
except:
print("some error...")
sys.exit()
else:
try:
sheet = workbook.sheet_by_index(0)
except IndexError as ie:
print(ie)
sys.exit()
else:
for rowx in range(sheet.nrows):
columnmapping = sheet.row_values(rowx)
yield columnmapping
def checkResult(self):
'''add to result only what i whant'''
for line in ReadXLS.realReadFilename(self):
if ((line[0] in (None, '')) or (line[1] in (None, ''))
or (line[2] in (None, '')) or not
re.search(r'[\w.-]+@[\w.-]+.\w+', line[2])):
continue
else:
#result[line[2].upper()].append((line[0], line[1]))
result[line[2].upper()].append(
(line[1].strip().replace(' ', ''), line[0].strip())
)
def __repr__(self):
''' Why I can '''
return "{}".format(self.filename)
if __name__ == '__main__':
test = ReadXLS(checkFilename(filename))
test.checkResult()
pprint(result)
24.09.2017 г.
Python compare data from sqlite database is between two date
#import module
import sqlite3
#database path is here
database = 'test.db'
#Testing in the data
#25.09.2017 27.09.2017
#01.10.2017 03.10.2017
class db_conn:
''' Class connect main '''
def __init__(self, db, Fdata=None, Sdata=None):
self.db = db
self.Fdata = Fdata
self.Sdata = Sdata
self.con = sqlite3.connect(self.db)
def insert(self):
''' insert into database two value's '''
self.con.execute("INSERT INTO praznik (Fdata, Sdata) VALUES (?, ?)", (self.Fdata, self.Sdata))
self.con.commit()
self.con.close()
def show_table(self):
''' Use for select from database '''
self.cur = self.con.cursor()
self.cur.execute("SELECT * from praznik")
for row in self.cur:
print("First Data {fdata} - Second Data {sdata}".format(fdata=row[1], sdata=row[2]))
self.con.close()
def check_period(self, fdata_check, ldata_check):
''' check period is valid or not '''
self.fdata_check = fdata_check
self.ldata_check = ldata_check
self.cur = self.con.cursor()
self.cur.execute("SELECT * from praznik")
for row in self.cur:
if row[1] <= self.ldata_check and row[2] >= self.fdata_check:
print("Problem")
break
else:
print("Look Good")
break
self.con.close()
import sqlite3
#database path is here
database = 'test.db'
#Testing in the data
#25.09.2017 27.09.2017
#01.10.2017 03.10.2017
class db_conn:
''' Class connect main '''
def __init__(self, db, Fdata=None, Sdata=None):
self.db = db
self.Fdata = Fdata
self.Sdata = Sdata
self.con = sqlite3.connect(self.db)
def insert(self):
''' insert into database two value's '''
self.con.execute("INSERT INTO praznik (Fdata, Sdata) VALUES (?, ?)", (self.Fdata, self.Sdata))
self.con.commit()
self.con.close()
def show_table(self):
''' Use for select from database '''
self.cur = self.con.cursor()
self.cur.execute("SELECT * from praznik")
for row in self.cur:
print("First Data {fdata} - Second Data {sdata}".format(fdata=row[1], sdata=row[2]))
self.con.close()
def check_period(self, fdata_check, ldata_check):
''' check period is valid or not '''
self.fdata_check = fdata_check
self.ldata_check = ldata_check
self.cur = self.con.cursor()
self.cur.execute("SELECT * from praznik")
for row in self.cur:
if row[1] <= self.ldata_check and row[2] >= self.fdata_check:
print("Problem")
break
else:
print("Look Good")
break
self.con.close()
3.09.2017 г.
Python use picke to write json to faile and read back to dictionary
#import module
from pprint import pprint # pretiprint
import json
import os
import pickle
# hard code param
test_file = 'test_file.json'
db = {}
dl = {}
def check_file_exists(filename):
''' Check file exists or not '''
if not os.path.exists(filename):
open(filename, 'w').close()
def write_in_json(filename, db):
''' Write dictionary to file with ekstension json'''
db = json.dumps(db)
with open(filename, 'wb') as f:
pickle.dump(db, f)
def load_from_json_file(filename, dl):
''' Load serial object into file to dictionary '''
json_file = open(filename, 'rb')
dl = pickle.load(json_file)
dlj = json.loads(dl)
return dlj
# Get from enywere for test
borko = {'name': 'Borko', 'ip': '192.168.168.1', 'df': 30000, 'free': 'dev'}
sue = {'name': 'Sue Somebody', 'ip': '192.168.168.2', 'df': 40000, 'free': 'hdw'}
tom = {'name': 'Tom Aks', 'ip': '192.168.168.3', 'df': 0, 'free': None}
db['bob'] = borko
db['sue'] = sue
db['tom'] = tom
if __name__ == '__main__':
# Create file if not exists uncoment to begin
# check_file_exists(test_file)
# function write json to file uncoment to begin
#write_in_json(test_file, db)
# fuction load picke file to json object return to dictionary
#print(load_from_json_file(test_file, dl))
from pprint import pprint # pretiprint
import json
import os
import pickle
# hard code param
test_file = 'test_file.json'
db = {}
dl = {}
def check_file_exists(filename):
''' Check file exists or not '''
if not os.path.exists(filename):
open(filename, 'w').close()
def write_in_json(filename, db):
''' Write dictionary to file with ekstension json'''
db = json.dumps(db)
with open(filename, 'wb') as f:
pickle.dump(db, f)
def load_from_json_file(filename, dl):
''' Load serial object into file to dictionary '''
json_file = open(filename, 'rb')
dl = pickle.load(json_file)
dlj = json.loads(dl)
return dlj
# Get from enywere for test
borko = {'name': 'Borko', 'ip': '192.168.168.1', 'df': 30000, 'free': 'dev'}
sue = {'name': 'Sue Somebody', 'ip': '192.168.168.2', 'df': 40000, 'free': 'hdw'}
tom = {'name': 'Tom Aks', 'ip': '192.168.168.3', 'df': 0, 'free': None}
db['bob'] = borko
db['sue'] = sue
db['tom'] = tom
if __name__ == '__main__':
# Create file if not exists uncoment to begin
# check_file_exists(test_file)
# function write json to file uncoment to begin
#write_in_json(test_file, db)
# fuction load picke file to json object return to dictionary
#print(load_from_json_file(test_file, dl))
Абонамент за:
Публикации
(
Atom
)