Initial commit

This commit is contained in:
Michael Reber
2019-11-15 12:59:38 +01:00
parent 40a414d210
commit b880c3ccde
6814 changed files with 379441 additions and 0 deletions

View File

@@ -0,0 +1,8 @@
def file_read(fname):
from itertools import islice
with open(fname, "w") as myfile:
myfile.write("Python Exercises\n")
myfile.write("Java Exercises")
txt = open(fname)
print(txt.read())
file_read('abc.txt')

View File

@@ -0,0 +1,6 @@
from collections import Counter
def word_count(fname):
with open(fname) as f:
return Counter(f.read().split())
print("Number of words in the file :",word_count("test.txt"))

View File

@@ -0,0 +1,6 @@
def file_lengthy(fname):
with open(fname) as f:
for i, l in enumerate(f):
pass
return i + 1
print("Number of lines in the file: ",file_lengthy("test.txt"))

View File

@@ -0,0 +1,7 @@
def longest_word(filename):
with open(filename, 'r') as infile:
words = infile.read().split()
max_len = len(max(words, key=len))
return [word for word in words if len(word) == max_len]
print(longest_word('test.txt'))

View File

@@ -0,0 +1,6 @@
def file_size(fname):
import os
statinfo = os.stat(fname)
return statinfo.st_size
print("File size in bytes of a plain file: ",file_size("test.txt"))

View File

@@ -0,0 +1,7 @@
def file_read(fname):
with open(fname) as f:
#Content_list is the list that contains the read lines.
content_list = f.readlines()
print(content_list)
file_read(\'test.txt\')

View File

@@ -0,0 +1,5 @@
def file_read(fname):
with open (fname, "r") as myfile:
data=myfile.readlines()
print(data)
file_read('test.txt')

View File

@@ -0,0 +1,9 @@
def file_read(fname):
content_array = []
with open(fname) as f:
#Content_list is the list that contains the read lines.
for line in f:
content_array.append(line)
print(content_array)
file_read('test.txt')

View File

@@ -0,0 +1,5 @@
def file_read(fname):
txt = open(fname)
print(txt.read())
file_read('test.txt')

View File

@@ -0,0 +1,6 @@
def file_read_from_head(fname, nlines):
from itertools import islice
with open(fname) as f:
for line in islice(f, nlines):
print(line)
file_read_from_head('test.txt',2)

View File

@@ -0,0 +1,19 @@
import sys
import os
def file_read_from_tail(fname,lines):
bufsize = 8192
fsize = os.stat(fname).st_size
iter = 0
with open(fname) as f:
if bufsize > fsize:
bufsize = fsize-1
data = []
while True:
iter +=1
f.seek(fsize-bufsize*iter)
data.extend(f.readlines())
if len(data) >= lines or f.tell() == 0:
print(''.join(data[-lines:]))
break
file_read_from_tail('test.txt',2)

View File

@@ -0,0 +1,7 @@
color = ['Red', 'Green', 'White', 'Black', 'Pink', 'Yellow']
with open('abc.txt', "w") as myfile:
for c in color:
myfile.write("%s\n" % c)
content = open('abc.txt')
print(content.read())