Reputation: 1
I'm coding a discord bot with Discord.py, and I don't know how to make this work.
@client.command(alisases = ['readfile'])
@commands.check(is_it_me)
async def jeff(ctx):
with open('Available.txt', 'r') as jeff_file:
for line in jeff_file:
await ctx.send(line)
The file contains 700 words, but I want it to send 5 or 10. Could anyone help me?
Upvotes: 0
Views: 281
Reputation: 15504
You can break a line into words using words = line.split()
, count the words, then rejoin the words into a single string using text = ' '.join(words)
.
The following code will send the first n
words of the file:
n = 5
@client.command(alisases = ['readfile'])
@commands.check(is_it_me)
async def jeff(ctx):
with open('Available.txt', 'r') as jeff_file:
words = []
while len(words) < n:
line = next(jeff_file) # will raise exception StopIteration if fewer than n words in file
words.append(line.split())
await ctx.send(' '.join(words[:n]))
The following code will read all words from the file, then select n words at random and send those:
import random
n = 5
@client.command(alisases = ['readfile'])
@commands.check(is_it_me)
async def jeff(ctx):
with open('Available.txt', 'r') as jeff_file:
words = [w for line in jeff_file for w in line.split()]
n_random_words = random.sample(words, n) # raises exception ValueError if fewer than n words in file
# n_random_words = random.choices(words, k=n) # doesn't raise an exception, but can choose the same word more than once if you're unlucky
await ctx.send(' '.join(n_random_words))
The following code will read and send the first n lines from the file:
n = 5
@client.command(alisases = ['readfile'])
@commands.check(is_it_me)
async def jeff(ctx):
with open('Available.txt', 'r') as jeff_file:
for _ in range(n):
line = next(jeff_file) # will raise exception StopIteration if fewer than n lines in file
await ctx.send(line)
n = 5
@client.command(alisases = ['readfile'])
@commands.check(is_it_me)
async def jeff(ctx):
with open('Available.txt', 'r') as jeff_file:
try:
for _ in range(n):
line = next(jeff_file)
await ctx.send(line)
except StopIteration:
pass
The following code will read the whole file and send n random lines:
import random
n = 5
@client.command(alisases = ['readfile'])
@commands.check(is_it_me)
async def jeff(ctx):
with open('Available.txt', 'r') as jeff_file:
all_lines = [line for line in jeff_file]
n_lines = random.sample(all_lines, n) # will raise exception ValueError if fewer than n words in file
# n_lines = random.choices(all_lines, k = n) # doesn't raise an exception, but might choose the same line more than once
await ctx.send(line)
Upvotes: 1
Reputation: 374
You can maybe use enumerate
and break the loop like this:
@client.command(alisases = ['readfile'])
@commands.check(is_it_me)
async def jeff(ctx):
with open('Available.txt', 'r') as jeff_file:
jeff_lines = jeff_file.readlines()
for i, line in enumerate(jeff_lines):
if i == 5:
break
else:
await ctx.send(line)
Upvotes: 0
Reputation: 4743
The other answers are works but if you want your code simpler and one line, you can use:
n = 5
@client.command(alisases = ['readfile'])
@commands.check(is_it_me)
async def jeff(ctx):
await ctx.send(' '.join(open('Available.txt', 'r').read().split(' ')[:n]))
This will send the first n
words from the document.
Upvotes: 0