problem: Write your own version of code that prompts for the name of the file to read, and the number of lines to print.
Can anyone help me to solve this problem in python?
I tried to like this :
import linecache
line=int(input("Line: "))
print(linecache.getline("Test.txt",line))
but can't solve the last problem where it said if the input is greater then the lines that already exist, all lines will be shown
file=input('File name: ')
print(os.access(file,os.F_OK))#check whether file is available or not
no_of_lines=int(input('Lines: '))
file_length=0
with open(file,'r') as f:
file_length=len(f.readlines())
with open(file,'r') as f:
if file_length<no_of_lines:
print(f.read())
else:
for i in range(no_of_lines):
print(f.readline().rstrip('\n'))
Related
I have an exercise which I am struggling a lot with related to file handling in python. I tried a lot of times but keep on failing to create the program. Those are the 2 questions.
1) Write a program that takes as input a phrase and a path to a text file on the computer. The program should return either true if the phrase is present in the file or false if the phrase is not present in the file. Can you please help me to figure out those 2 questions. Thanks in advance.
2) Write a program that reads a csv file, where each line contains a coma-separated list of numbers and writes the sum of each line in another file. Use try-catch to handle potential errors such as empty lines or non-numeric values.
For question 1 I wrote:
phrase = input("Enter a phrase: ")
file = open("example.txt", "r")
if phrase in file:
print("True")
else:
print("False")
For question 2 I wrote:
file = open("example.csv", "r")
print(f.readlines())
try:
for i in file:
line = line + 1
file2 = open("lines.csv", "w")
file2.write(line)
file2.close
Code:
opener = open("gymclub.txt", "r")
for line in opener:
print(line)
Output:
Alex,45,7
Kier,67,18
Ben,66,19
Jack,75,29
Harry,25,1
Zeki,8,0
Dagnis,10,2
Ryan,33,15
Tobilola,30,9
Chris,74,24
Sebastian,73,28
I need it to print these lines but without the empty lines in between. I've had this problem before and I've solved it but I can't remember what the solution was. I can't find any solutions online.
Use this:
print(line, end ="")
Or you can use this:
import sys
sys.stdout.write(line)
I want to know how I can search for a specific word in a .txt file using Python3.
If the word 'pizza' is included in the .txt file, then it should (e.g.) import another python program.
You can iterate through each line in the file with a 'for' loop and check to see if the string is in the line:
f = open('file.txt', 'r')
for line in f:
if 'pizza' in line:
# do whatever you want here
print("Found it!")
else:
pass
import os,sys
import time
from colorama import Fore,Back,Style,init
init(autoreset=True)
appdata_path = os.path.join(os.getenv("APPDATA"), os.pardir)
subpath = "Local/sieosp/filesav2292.sav"
f = open(os.path.join(appdata_path, subpath), "r+")
lines=f.readlines()
a1=int (lines[116])
a2=int (lines[120])
a3=int (lines[124])
b4=int (lines[128])
c5=int (lines[132])
d6=int (lines[136])
e7=int (lines[140])
d8=int (lines[144])
d9=int (lines[148])
d10=int (lines[152])
d11=int (lines[156])
d12=int (lines[160])
total=int (a1+a2+a3+b4+c5+d6+e7+d8+d9+d10+d11+d12)
if (total)==(12):
print("You already own every character")
else:
with f:
userinputvalue=int (input("Type 1 if you want to unlock every character,or 0 if you would like to close this \n"))
if(userinputvalue)==1:
lines[156]=f.write("1\n")
lines[116]=f.write("1\n")
lines[120]=f.write("1\n")
lines[124]=f.write("1\n")
lines[128]=f.write("1\n")
lines[132]=f.write("1\n")
lines[136]=f.write("1\n")
lines[140]=f.write("1\n")
lines[144]=f.write("1\n")
lines[148]=f.write("1\n")
lines[152]=f.write("1\n")
lines[160]=f.write("1\n")
else:
print("Closing")
time.sleep(1)
So this should work,right? Don't know why f.write doesn't write 1 to my file. am i using it very wrong? Searched around google for some more info but I didnt understand a thing :/ tried to use f.write as f.readlines but no luck. thanks
It looks like you dont open the file in write mode, only in read mode.
f = open(os.path.join(appdata_path, subpath), "r+")
Change the "r" to a "w"
You have opened the file with "r+", so the file is even writable, the problem is that if you open a file with "r+" you have to manage the pointer in the file, otherwise the string will be append at the end.
In order to manage it you have to use the function f.seek(offset, from_what) like described here Input and Output.
For example in this code I change only the first line of the file:
f = open("File/Path/file.txt", "r+")
f.seek(0,0)
f.write("something")
f.close()
You also use line[N] = f.write("something"), careful to use it in this way, because it returns the number of characters wrote, not the characters wrote ;)
I wish to write a program that can read a file and if a particular str_to_find is found in a bigger string, say
"AACATGCCACCTGAATTGGATGGAATTCATGCGGGACACGCGGATTACACCTATGAGCAGAAATACGGCCTGCGCGATTACCGTGGCGGTGGACGTTCTTCCGCGCGTGAAACCGCGATGCGCGTAGCGGCAGGGGCGATCGCCAAGAAATACCTGGCGGAAAAGTTCGGCATCGAAATCCGCGGCTGCCTGACCCAGATGGGCGACATTCCGCTGGAGATTAAAGACTGGCGTCAGGTTGAGCTTAATCCGTTTTC"
then write that line and the above line of it into the file and keep repeating it for all the match found.
Please suggest the solution. I have written the program for printing that particular search line but I don't know how to write the above line.
import re
import string
file=open('C:/Users/Administrator/Desktop/input.txt','r')
output=open('C:/Users/Administrator/Desktop/output.txt','w')
count_record=file.readline()
str_to_find='AACCATGC'
while count_record:
if string.find(list,str_to_find) ==0:
output.write(count_record)
file.close()
output.close()
one way
for line in open("file"):
if "str_to_find" in line:
print prev
print line.rstrip()
prev=line.rstrip()