set row number to document and get data in python - python

I have a series of files and I have to separate and display part of the text
my code is :
path = 'C:\\Bot\\*.log'
files = glob.glob(path)
nlines = 0
for name in files:
try:
with open(name) as f:
for line in f :
nlines += 1
if (line.find("Total") >= 0):
print(line)
I need a text that is saved in the file after the line number is obtained.
With the above code, I have access to the line number but I do not have access to some subsequent lines
How to access the next line value??

path = 'C:\\Bot\\*.log'
files = glob.glob(path)
nlines = 0
for name in files:
try:
with open(name) as f:
for line in f:
nlines += 1
if (line.find("Total") >= 0):
print(next(f))
I think it is a better solution to this problem

Use next() to read:
path = 'C:\\Bot\\*.log'
files = glob.glob(path)
nlines = 0
for name in files:
try:
with open(name) as f:
for line in f:
nlines += 1
if (line.find("Total") >= 0):
for i in range(6):
print(next(f))

Related

Script that should remove certain txt files from a directory does not remove the files

I am trying to write a python script that iterates over all the txt files in a directory and deletes those that have fewer words than a defined limit. The current script does not delete the files. Below is what I tried:
import os
wordLimit = 1000
def articleWordCount(filename):
number = 0
with open(filename, 'w+') as f:
for line in f:
words = line.split()
number += len(words)
return number
def articlesRemoval():
directory = 'data'
removedArticles =0
for filename in os.listdir(directory):
if filename.endswith(".txt"):
if articleWordCount(filename) < wordLimit:
removedArticles += 1
os.remove(filename)
else:
print(os.path.join(directory, filename))
print(removedArticles)
articlesRemoval()
You should open the file in reading mode with the option "r+", you are opening it in write mode and the function articleWordCount always returns 0.
os.listdir() doesn't return paths, only filenames, so the files that you are trying to remove do not exist... I am assuming that data is in the directory where you are starting the script and that it does find the files you want. Change os.remove(filename) to os.remove(os.path.join(directory, filename)):
import os
wordLimit = 1000
def articleWordCount(filename):
number = 0
with open(filename, 'w+') as f:
for line in f:
words = line.split()
number += len(words)
return number
def articlesRemoval():
directory = 'data'
removedArticles = 0
for filename in os.listdir(directory):
if filename.endswith(".txt"):
if articleWordCount(filename) < wordLimit:
removedArticles += 1
os.remove(os.path.join(directory, filename))
else:
print(os.path.join(directory, filename))
print(removedArticles)
articlesRemoval()

Python edit line from txt file

I'm making program that open txt file and replace first 0 with 1 of given line. Now it only print the edited line, but I want that it prints all the lines. I'm using python 3.1.
line_number = 3
with open(filename, "r") as f:
number = 0
for line in f:
number += 1
if line_number == number:
content = line.replace("0","1",1)
savefile = filename[:4] + ".tmp"
with open(savefile, "w") as f:
f.write(content)
os.remove(filename)
os.rename(savefile, filename)
Text file:
0 Dog
0 Cat
0 Giraffe
0 Leopard
0 Bear
You need to write each unchanged line to the savefile:
import os
filename = 'input.txt'
line_number = 3
savefile = filename[:4] + ".tmp"
with open(filename, "r") as f:
with open(savefile, "w") as fout:
number = 0
for line in f:
number += 1
if line_number == number:
content = line.replace("0","1",1)
fout.write(content)
else:
# Write unchanged lines here
fout.write(line)
os.remove(filename)
os.rename(savefile, filename)
Did you try something like this:
filename = "./test.txt"
with open(filename) as f:
lines = f.readlines()
# the element with index 2 is the 3-th element
lines[2] = lines[2].replace("0","1",1)
with open(filename, 'w') as f:
[f.write(line) for line in lines]
Output(./test.txt):
0 Dog
0 Cat
1 Giraffe
0 Leopard
0 Bear
You can read the file and save it to a list. Then you can then perform a certain action for each item(or for a specific element) in the list and save the result in the same file. You don't need of .tmp file or to remove and rename a file.
Edit:
There is an another approach with fileinput (thanks to #PeterWood)
import fileinput
with fileinput.input(files=('test.txt',), inplace=True) as f:
for line in f:
if fileinput.lineno() is 3:
print(line.replace("0", "1", 1).strip())
else:
print(line.strip())

Python how to cap a string from files path

In python2, how do I limit the length of a string from importing all txt files from a directory? like wordlength = 6000
import glob
raw_text = ""
path = "/workspace/simple/*.txt"
for filename in glob.glob(path):
with open(filename, 'r') as f:
for line in f:
raw_text += line
words = raw_text.split()
print(words)
this code only feeds in all txt files and prints in on screen. How do I limit it to 6000 words and only prints 6000 words?
import glob
raw_text = ""
path = "/workspace/simple/*.txt"
for filename in glob.glob(path):
with open(filename, 'r') as f:
for line in f:
if len(raw_text.split())< N: ###here you put your number
raw_text += line
else:
break
words = raw_text.split()
print(words)
Assuming you are wanting 6000 or less words from each file ?
import glob, sys
path = sys.argv[1]
count = int(sys.argv[2]) if len(sys.argv) > 2 else 60
words = []
for file in glob.glob(path):
with open(file) as f:
words += f.read().split()[:count]
print(words)
>>>python test.py "/workspace/simple/*.txt" 6000
You could also set up a dictionary for words to file :
import glob, sys
path = sys.argv[1]
count = int(sys.argv[2]) if len(sys.argv) > 2 else 60
fwords = {}
for file in glob.glob(path):
with open(file) as f:
fwords[file] = f.read().split()[:count]
print(fwords)
If you want only files with the count of words in them
for file in glob.glob(path):
with open(file) as f:
tmp = f.read().split()
if len(tmp) == count : # only the count
fwords[file] = tmp
That depends on your definition of a word. If it's simply text separated by white space, it's fairly easy: count the words as they go past, and stop when you have enough. For instance:
word_limit = 6000
word_count = 0
for line in f:
word_count += len(line.split())
if word_count > word_limit:
break
raw_text += line
If you want exactly 6000 words, you can modify the loop to grab enough words from the last line to make 6000 exactly.
If you want to make it a little more effective, then drop raw_text and build words within the loop, one line at a time, with
line_words = line.split()
words.extend(line_words)
In this case, you'll want to use len(line_words) for your check.
Try replacing your code with this:
for filename in glob.glob(path):
with open(filename, 'r') as f:
word_limit = 12000
word_count = 0
for line in f:
word_count += len(line)
if word_count > word_limit:
break
raw_text += line

Python - Mean Length Function for All Files in Folder

I have a function that finds the mean string length. Now I'm trying to write a function that will traverse and touch every txt file in the directory and return the file with the highest mean. What I've got right now doesn't seem to traverse properly. Pleeease help. Thanks.
from __future__ import print_function
import os
def mean_length(file path):
length = 0.0
line_num = 0
with open(filepath) as f:
for line in f:
if line.strip():
length += len(line.strip())
line_num += 1
return length/line_num
def highest_mean():
max_mean = 0
max_name = ""
filepath = open("Desktop/Textfiles/moby.txt")
for root, dirs, files in os.walk("Desktop/Textfiles"):
for filename in files:
if filename.endswith('.txt'):
filepath = os.path.join(root, filename)
if mean_length(filepath) > max_mean:
max_name = filename
max_mean = mean_length(filepath)
return max_name
I think you need to go through all files to get the one with the highest mean, maybe with another two variables:
def mean_length(filepath):
length = 0.0
line_num = 0
with open(filepath) as f:
for line in f:
if line.strip():
length += len(line.strip())
line_num += 1
return length/line_num
def highest_mean():
max_mean = 0
max_name = ""
for root, dirs, files in os.walk("Desktop/Textfiles"):
for filename in files:
if filename.endswith('.txt'):
filepath = os.path.join(root, filename)
m_length = mean_length(filepath)
if m_length > max_mean:
max_name = filename
max_mean = m_length
return max_name
This is a simple code do the same work of len() built in function.
var =input("enter your text to calculate here : ")
def length(var):
count =0
for i in var:
count +=1
print(count)
lent(var)
print(len(var))

Deleting one line within txt file in Python

I am having problems deleting a specific line/entry within a text file. With the code I have the top line in the file is deleted no matter what line number I select to delete.
def erase():
contents = {}
f = open('members.txt', 'a')
f.close()
f = open('members.txt', 'r')
index = 0
for line in f:
index = index + 1
contents[index] = line
print ("{0:3d}) {1}".format(index,line))
f.close()
total = index
entry = input("Enter number to be deleted")
f = open('members.txt', 'w')
index = 0
for index in range(1,total):
index = index + 1
if index != entry:
f.write(contents[index])
Try this:
import sys
import os
def erase(file):
assert os.path.isfile(file)
with open(file, 'r') as f:
content = f.read().split("\n")
#print content
entry = input("Enter number to be deleted:")
assert entry >= 0 and entry < len(content)
new_file = content[:entry] + content[entry+1:]
#print new_file
with open(file,'w') as f:
f.write("\n".join(new_file))
if __name__ == '__main__':
erase(sys.argv[1])
As already noted you were starting the range from 1 which is incorrect. List slicing which I used in new_file = content[:entry] + content[entry+1:] makes the code more readable and it is an approach less prone to similar errors.
Also you seem to open and close the input file at the beginning for no reason. Also you should use with if possible when doing operations with files.
Finally I used the join and split to simplify the code so you don't need a for loop to process the lines of the file.

Categories