Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I checked the repository using black(plugin) #2201

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion 1 File handle/File handle binary/Update a binary file2.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def update():
value = pickle.load(File)
found = False
roll = int(input("Enter the roll number of the record"))

for i in value:
if roll == i[0]:
print(f"current name {i[1]}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
Write a function remcount( ) to count the number of students who need
remedial class (student who scored less than 40 percent)


"""

# also find no. of children who got top marks

import pickle
Expand Down
9 changes: 5 additions & 4 deletions 1 File handle/File handle text/counter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
- Code readability
"""


class Counter:

def __init__(self, text:str) -> None:
def __init__(self, text: str) -> None:
self.text = text

# Define the initial count of the lower and upper case.
Expand All @@ -16,20 +17,20 @@ def __init__(self, text:str) -> None:
self.count()

def count(self) -> None:

for char in self.text:
if char.lower():
self.count_lower += 1
elif char.upper():
self.count_upper += 1

return (self.count_lower, self.count_upper)

def get_total_lower(self) -> int:
return self.count_lower

def get_total_upper(self) -> int:
return self.count_upper

def get_total(self) -> int:
return self.count_lower + self.count_upper
return self.count_lower + self.count_upper
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@

import os
import time
file_name= input("Enter the file name to create:- ")

file_name = input("Enter the file name to create:- ")

print(file_name)


def write_to_file(file_name):

if os.path.exists(file_name):
Expand All @@ -15,24 +16,26 @@ def write_to_file(file_name):

while True:
text = input("enter any text to add in the file:- ")
F.write( f"{text}\n" )
F.write(f"{text}\n")
choice = input("Do you want to enter more, y/n").lower()
if choice == "n":
break



def longlines():

with open(file_name, encoding='utf-8') as F:
with open(file_name, encoding="utf-8") as F:
lines = F.readlines()
lines_less_than_50 = list( filter(lambda line: len(line) < 50, lines ) )
lines_less_than_50 = list(filter(lambda line: len(line) < 50, lines))

if not lines_less_than_50:
print("There is no line which is less than 50")
else:
for i in lines_less_than_50:
print(i, end="\t")


if __name__ == "__main__":
write_to_file(file_name)
time.sleep(1)
longlines()
longlines()
2 changes: 1 addition & 1 deletion 1 File handle/File handle text/happy.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ Take a walk in the park
At the end of the day you are more important than anything else.
Many moments of happiness are waiting
You are amazing!
If you truly believe.
If you truly believe.
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
sys.stdout.write("Enter the name of the file")
file = sys.stdin.readline()

with open(file.strip(), ) as F:
with open(file.strip()) as F:

while True:
ch = F.readlines()
for (i) in ch: # ch is the whole file,for i in ch gives lines, for j in i gives letters,for j in i.split gives words
for (
i
) in (
ch
): # ch is the whole file,for i in ch gives lines, for j in i gives letters,for j in i.split gives words
print(i, end="")
else:
sys.stderr.write("End of file reached")
break

21 changes: 10 additions & 11 deletions 1 File handle/File handle text/question 2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,32 @@
using read function
and display those words, which are less than 4 characters. """

print("Hey!! You can print the word which are less then 4 characters")

print("Hey!! You can print the word which are less then 4 characters")

def display_words(file_path):

try:
with open(file_path) as F:
words = F.read().split()
words_less_than_40 = list( filter(lambda word: len(word) < 4, words) )
words_less_than_40 = list(filter(lambda word: len(word) < 4, words))

for word in words_less_than_40:
print(word)

return "The total number of the word's count which has less than 4 characters", (len(words_less_than_40))


return (
"The total number of the word's count which has less than 4 characters",
(len(words_less_than_40)),
)

except FileNotFoundError:
print("File not found")


print("Just need to pass the path of your file..")

file_path = input("Please, Enter file path: ")

if __name__ == "__main__":

print(display_words(file_path))





print(display_words(file_path))
22 changes: 14 additions & 8 deletions 1 File handle/File handle text/question 5.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
"""Write a function in python to count the number of lowercase
alphabets present in a text file “happy.txt"""

import time, os
import os
import time

from counter import Counter

print("You will see the count of lowercase, uppercase and total count of alphabets in provided file..")
print(
"You will see the count of lowercase, uppercase and total count of alphabets in provided file.."
)


file_path = input("Please, Enter file path: ")

if os.path.exists(file_path):
print('The file exists and this is the path:\n',file_path)
print("The file exists and this is the path:\n", file_path)


def lowercase(file_path):
try:

with open(file_path) as F:
word_counter = Counter(F.read())

print(f"The total number of lower case letters are {word_counter.get_total_lower()}")

print(
f"The total number of lower case letters are {word_counter.get_total_lower()}"
)
time.sleep(0.5)
print(f"The total number of upper case letters are {word_counter.get_total_upper()}")
print(
f"The total number of upper case letters are {word_counter.get_total_upper()}"
)
time.sleep(0.5)
print(f"The total number of letters are {word_counter.get_total()}")
time.sleep(0.5)
Expand All @@ -30,8 +38,6 @@ def lowercase(file_path):
print("File is not exist.. Please check AGAIN")




if __name__ == "__main__":

lowercase(file_path)
12 changes: 9 additions & 3 deletions 1 File handle/File handle text/question 6.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@

from counter import Counter


def lowercase():

with open("happy.txt") as F:
word_counter = Counter(F.read())

print(f"The total number of lower case letters are {word_counter.get_total_lower()}")
print(f"The total number of upper case letters are {word_counter.get_total_upper()}")

print(
f"The total number of lower case letters are {word_counter.get_total_lower()}"
)
print(
f"The total number of upper case letters are {word_counter.get_total_upper()}"
)
print(f"The total number of letters are {word_counter.get_total()}")


if __name__ == "__main__":
lowercase()
16 changes: 10 additions & 6 deletions 1 File handle/File handle text/question3.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

import os
import time
file_name= input("Enter the file name to create:- ")

file_name = input("Enter the file name to create:- ")

# step1:
print(file_name)



def write_to_file(file_name):

if os.path.exists(file_name):
Expand All @@ -21,11 +21,12 @@ def write_to_file(file_name):

while True:
text = input("enter any text")
F.write(f"{text}\n")
F.write(f"{text}\n")

if input("do you want to enter more, y/n").lower() == "n":
break



# step2:
def check_first_letter():
with open(file_name) as F:
Expand All @@ -37,10 +38,13 @@ def check_first_letter():
count_i = first_letters.count("i")
count_m = first_letters.count("m")

print(f"The total number of sentences starting with I or M are {count_i + count_m}")
print(
f"The total number of sentences starting with I or M are {count_i + count_m}"
)


if __name__ == "__main__":

write_to_file(file_name)
time.sleep(1)
check_first_letter()
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
with open("happy.txt", "r") as F:
with open("happy.txt") as F:
# method 1
for i in F.read().split():
print(i, "*", end="")
Expand Down
4 changes: 2 additions & 2 deletions 1 File handle/File handle text/story.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
once upon a time there was a king.
he was powerful and happy.
he
he
all the flowers in his garden were beautiful.
he lived happily ever after.
he lived happily ever after.
Loading