check if a file is modified and print the modified line - python

i tried to make auto check if file is modified then print and send to discord using webhook, but it print the last line and before the last line. i just want to print the last line. here is the code
def readfile():
with open("test.txt", "r") as t:
read = t.readlines()
return read
before = readfile()
while True:
after = readfile()
if before != after:
for line in after:
if line not in before:
print(line)
sendembed()
before = after
im new to python, anyone can help me?

given that the first part is answered with the link: How do I watch a file for changes?
This will answer the second part, which the question was close on.
def readfile():
with open(`\test.txt`, "r") as t:
read = t.readlines()
return read
x = readfile()
# this is the last line
print( x[-1] )
The reason for this is that readlines() creates a list and the user can access the last element of the list.

Related

Load Email:Pass from a file

file = open("list.txt", "r", encoding="Latin-1").readlines()
file = [combos.rstrip() for combos in file]
for lines in file:
data = lines.split(":")
password = data[1]
email = data[0]
print(email)
my list.txt looks like this:
test#gmail.com:Password1
123#gmail.com:Pass
842398412#Gmail.com:daidn
Output:
842398412#Gmail.com
I want it to move down to the next line each time I call email so, for example,
if I do print(email) I want it to output the first email in the list and if I call it again I want to output the second one. Is this possible?
To look for the next line when running your code again, your code needs to keep track of what line it should be looking for. You can write a generator that keeps track of this for you, and you can call next() to get each next value in the list.
def file_lines():
lines = open("list.txt", "r", encoding="Latin-1").readlines()
for line in lines:
yield line
generator = file_lines()
print(next(generator))
print(next(generator))
print(next(generator))
Alternatively just store it in a list and don't use a generator at all
My recommendations:
Don't recommended to open files without context managers
You can do a function yield...
You can convert a list to an iter.
I prefer last:
with open("list.txt", "r", encoding="Latin-1") as file:
content = [i.rstrip() for i in file.readlines()]
for lines in content:
data = lines.split(":")
password = data[1]
email = data[0]
items.append(email)
emails = iter(items)
And then next(email) for iterate above it
Good luck

How to prompt user that asks a user for a file name?

I am going through Intro to Programming so basic stuff here, I have an assignment to "write a program that asks a user for a file name and then displays the first 5 lines of the file," I just can't figure out how to use the input command in this situation and then transfer to open()
Edit: Sorry here is a code snippet I had, I just don't get how to apply input from here.
def main():
#This function writes to the testFile.docx file
outfile = open('testFile.docx', 'w')
outfile.write('Hello World\n')
outfile.write('It is raining outside\n')
outfile.write('Ashley is sick\n')
outfile.write('My dogs name is Bailey\n')
outfile.write('My cats name is Remi\n')
outfile.write('Spam Eggs and Spam\n')
outfile.close()
infile = open('testFile.docx', 'r')
testFileContent = infile.read()
infile.close()
print(testFileContent)
main()
First, we ask for a filename. Then we use the try clause, which checks whether the file exists. If it does it will print 5 lines. If it does not, it will print No such a file found!
x = input('Enter a file name')
try:
with open(x) as f:
data = f.readlines()
for i in range(5):
print(data[i])
except:
print('No such a file found!')
Using a simple function,
def hello_user():
user_input = input('Enter file name: ')
try:
with open(user_input, 'r') as f:
data = f.readlines()
data = data[:5]
for o in data:
print(o.strip())
except FileNotFoundError:
print('Not found ')
hello_user()
It asks for a file name
If the file exists in the same directory the script is running, it opens the file and read each lines (white lines inclusive)
We select only the first 5 lines
We iterate through the list and remove the extra whitespace character(e.g \n).
If the file was not found, we catch the exception.
input() is used to receive input from the user. Once we recieve the input, we use the open() method to read the file in read mode.
def main():
file = input("Please enter a file name")
with open(file, 'r') as f:
lines = f.readlines()
print(lines[:5])
The with statement makes sure that it closes the file automatically without explicitly calling f.close()
The method f.readlines() returns an array containing the lines in the file.
The print() statement prints the first 5 lines of the file.

trying to get random line from text document (python)

I'm new to python and was working on a discord bot with Discord.py. I was trying to make it so when you type %quote it would get a random line from a text file. but for some reason it skips the first line or two. maybe I'm getting the logic wrong? here's my code.
#commands.command()
async def quote(self, ctx, Qnum='janfol91213sdf2uieh1あ2Ⅳ3sんksnaaksd'):
i = 0
if Qnum == 'janfol91213sdf2uieh1あ2Ⅳ3sんksnaaksd':
f = open("cogs\Base\data\Quotes.txt")
random_lines = random.choice(f.readlines())
await ctx.send(random_lines)
else:
f = open("cogs\Base\data\Quotes.txt")
for line in f:
if i == int(Qnum):
quote = f.readline()
f.close()
break
else:
i = i+1
await ctx.channel.send(quote)
f.close()
the random Qnum thing is for optional parameter or something
Assuming your txt file looks something like this:
quote1
quote2
you could read the txt file and split it with \n, like this:
import random
with open('file.txt', 'r') as f:
read = f.read()
array = read.split('\n')
quote = random.choice(array)
await ctx.channel.send(quote)
to make sure it doesnt skip any lines, you can print the array with the quotes to the console, with print(array)

Unable to move to second function after the result of the first

Program Goal: Search a defined yamlfile (scan_dcn.yaml) and return all lines matching the search criteria as defined in the function_search_search_key() and function_search_event_type() functions.
Input File - scan_dcn.yaml:
search_dict:
[
{search_key: ["Failed to Process the file"],
event_type: "evttyp_repl_dcn_error",
event_description: "Failure to process DCN file",
priority: 50,
scan_interval: 1,
remove_dups: True,
category: "dcn",
context_begin: 0,
context_end: 1,
reportable: False,
offset: 0
},
Problem:
My program will return function_search_search_key() but will not proceed to function_search_event_type().
I would think that my problem is that I have no logic to proceed to the second function after the completion of the first.
Do I need to return a value in each function to proceed?
Python Source Code
yamlfile = open('scan_dcn.yaml', 'r')
def function_search_search_key():
search_search_key = ['{search_key:']
for line in yamlfile.readlines():
for word in search_search_key:
if word in line:
print(line)
def function_search_event_type():
search_event_type = ['event_type:']
for line in yamlfile.readlines():
for word in search_event_type:
if word in line:
print(line)
def main():
function_search_search_key()
function_search_event_type()
main()
In your first function you read the whole file with readlines. When you use readlines again in your second function you're already at the end of the file and there is no more data to read, so the for loop is not even entered.
But there's no need to read the file again for every function. Read the file outside of the functions and put it in a list. Then add a parameter to each of those functions that takes this list. In the function you can loop over the list.
def function_search_search_key(lines):
search_search_key = ['{search_key:']
for line in lines:
for word in search_search_key:
if word in line:
print(line)
def function_search_event_type(lines):
search_event_type = ['event_type:']
for line in lines:
for word in search_event_type:
if word in line:
print(line)
def main():
with open('scan_dcn.yaml', 'r') as yamlfile:
lines = yamlfile.readlines()
function_search_search_key(lines)
function_search_event_type(lines)
if __name__ = '__main__':
main()
If you ever need to change the name of the file you can do it in one place. If you open and read the file in every single function you would have to change all occurrances of the file name.
Your second function is being entered. It must if the call above it has finished.
You aren't seeing anything printed because you're attempting to loop though the same file more than once. Once you've read the file, it's exhausted. You can just re-read the file as a simple fix:
def function_search_search_key():
with open('scan_dcn.yaml', 'r') as yamlfile:
search_search_key = ['{search_key:']
for line in yamlfile.readlines():
for word in search_search_key:
if word in line:
print(line)
def function_search_event_type():
with open('scan_dcn.yaml', 'r') as yamlfile: # Read the file again
search_event_type = ['event_type:']
for line in yamlfile.readlines():
for word in search_event_type:
if word in line:
print(line)
You can read a file descriptor only once (if you don't seek to start), so you may open your file in each function
def function_search_search_key():
search_search_key = ['{search_key:']
with open('scan_dcn.yaml') as fic:
for line in fic:
for word in search_search_key:
if word in line:
print(line)
def function_search_event_type():
search_event_type = ['event_type:']
with open('scan_dcn.yaml') as fic:
for line in fic:
for word in search_event_type:
if word in line:
print(line)

how to read line by line from a textfile and pass the value using python

I have a file "BatchLink.txt" which contains urls in new lines.I want to read these lines by lines and pass the argument linewise to a batch script.
Let's say, my batchlink.txt contains this data :-
http://link1
http://link2
http://link3
http://link4
Now, I want to read linewise and send this to a batch file one line at a time.This question is the continuation of my previous question Here.
Right Now, I have this code :-
import requests
from bs4 import BeautifulSoup
import subprocess
file = open("BatchLinks.txt", "w")
r = requests.get('http://www.crunchyroll.com/i-cant-understand-what-my-husband-is-saying')
soup = BeautifulSoup(r.text)
print soup.title
subtitles = soup.findAll('div',{'class':'wrapper container-shadow hover-classes'})
for episode in subtitles:
x = episode.find_all('a')
for a in x:
#print a['href']
z = a['href']
l = 'http://www.crunchyroll.com'+ z
print l
file.write(l+'\n')
print '-----------------------------------------------------------------------------'
file.close()
file = open('BatchLinks.txt', "r")
lines = file.readlines()
print lines
if __name__ =="__main__":
response = lines
print(lines)
p = subprocess.Popen("_start.bat", stdin = subprocess.PIPE)
time.sleep(1)
p.stdin.write(response) #Answer the question
time.sleep(20)
But, right now, the problem is that it reads the lines simultaneosuly and send it to the batch file.Which returns me the output [] . I can't seem to get it work.Any help/guidance would be appreciated.
file = open('BatchLinks.txt', "r")
lines = file.readlines()
Change that to a more up-to-date version:
with open('BatchLinks.txt', "r") as inf:
for line in inf:
do something with line
file = open('BatchLinks.txt', "r")
lines = file.readlines()
Change that to a more up-to-date version:
with open('BatchLinks.txt', "r") as inf:
for line in inf:
do something with line
This is very basic stuff. Use the manual! https://docs.python.org/2/library/stdtypes.html#file-objects
I see two issues: you need to write to the pipe line by line, and need to close the pipe after done.
Replace the following line:
p.stdin.write(response) #Answer the question
with this one:
for line in response:
p.stdin.write(line)
p.stdin.close()
When you do - lines = file.readlines() - it reads all the lines from the file and returns it as a list , so lines is a list of all the lines in the file.
Then you are doing - p.stdin.write(response) - this sends the complete list over to the other process, you should iterate over the lines and send each line to a new process.
Example -
if __name__ =="__main__":
for line in lines:
print(line)
p = subprocess.Popen("_start.bat", stdin = subprocess.PIPE)
time.sleep(1)
p.stdin.write(line.strip())
time.sleep(20)

Categories