This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I am Programming in python however i have come to a slight glitch which i cannot solve! The issue is that when it prints out in a text files it only prints one line of the whole output! Otherwise it works! Please i need help to make this work!
import sys, bz2, string, os
#instead of hardcoding filename, get it from arguments
#filename = os.getcwd()
filename = raw_input("Enter the path of bz2 document e.g. files/access_log-20130301.bz2: ")
print "Using file : " + filename
source_file = bz2.BZ2File(filename, "r")
for line in source_file:
#Extract the date and put into a variable
logdate = string.split(line)[3][1:12]
#Extract movie name and put into variable movie
movie = string.split(line)[6]
#extract who read the movie username =
usernames = string.split(line)[2]
#Only process the movie line if we have /media/movie in it.
if movie.find('media/movies') > 0:
#Prints all things prosscesed
print "User:" + usernames + " On:" + logdate + " Was watching:"+ movie
#p=open(filename+"record.txt", "w")
fp=open(filename+"record.txt", "wb+")
fp.write("User: " + usernames + " On: " + logdate + " Was watching: "+ movie+" File from:"+filename+"\n")
sys.exit()
The problem is likely that you are opening a new file handle for the file each time you want to write a line, and you do not flush the output first. There are two possible solutions here:
Open the file you intend to write to before your main for loop. This way you will only have one file handle, and a lack of flushing will not cause this behavior. Make sure you close the file when you are done. (Consider using a with block, which will cause the file to be closed automatically at the termination of the block: with open(filename + "record.txt", "wb+") as f:)
Close fp immediately after the fp.write() call, which will force any buffered output to be flushed, at least to the kernel I/O cache.
I would prefer option 1, as there is no reason to open and close the file multiple times in this case. (If you are writing many lines to the file, these open/flush/close cycles will wind up wasting a lot of time!)
Option 1 would look something like this:
import sys, bz2, string, os
#instead of hardcoding filename, get it from arguments
#filename = os.getcwd()
filename = raw_input("Enter the path of bz2 document e.g. files/access_log-20130301.bz2: ")
print "Using file : " + filename
with open(filename+"record.txt", "wb+") as fp:
source_file = bz2.BZ2File(filename, "r")
for line in source_file:
#Extract the date and put into a variable
logdate = string.split(line)[3][1:12]
#Extract movie name and put into variable movie
movie = string.split(line)[6]
#extract who read the movie username =
usernames = string.split(line)[2]
#Only process the movie line if we have /media/movie in it.
if movie.find('media/movies') > 0:
#Prints all things prosscesed
print "User:" + usernames + " On:" + logdate + " Was watching:"+ movie
#p=open(filename+"record.txt", "w")
fp.write("User: " + usernames + " On: " + logdate + " Was watching: "+ movie+" File from:"+filename+"\n")
# The with block has ended at this point, so the file will already be closed here.
sys.exit()
You're opening the output file in write mode inside the loop. Open it once outside your main loop.
Be sure to close it when you're done. Even better, write that like:
with open(filename + "record.txt", "wb+") as fp:
for line in source_file:
...
fp.write(...)
so that the open context manager closes it for you afterward.
Related
I am writing a Python program where I need to write to a file. I need an if condition to determine if I need to keep writing to same file or open a new file. How do I declare the file so that I can access it with both the if and else? Right now I'm making a test file before the loop just so I have access to the variable. How to avoid opening a TEST.txt file while still having a variable f that I can operate on?
f = open(outputFolder + "TEST.txt", 'w') # how to avoid opening TEST.txt here
while row:
#print(str(row[0]) + '|' + str(row[4]))
currentFileName = getFileName(str(row[0]))
# If coming up on new date open new file
if currentFileName != fileName:
f.close()
fileName = currentFileName
print("Processing: " + fileName)
f = open(outputFolder + fileName, 'w')
f.write(getLine(row))
# else write to current file
else:
f.write(getLine(row))
row = cursor.fetchone()
You didn't work out your logic before writing the program. What you describe in words does not match what you wrote. Start with the words, draw a flowchart, and then write your code.
In your posted program, you're trying to open currentFile multiple times, you don't initialize or change row, and it's not clear what you intend the program to do.
if [condition]:
filename = currentFileName
else:
filename = "TEST.txt"
f = open(filename)
for ... # whatever you're trying to do with your input and output,
# look up the proper method in a file-handling tutorial.
I'm writing a simple function that takes the path of a text file and returns the number of lines contained in that file.
I've made sure to set the file pointer to the beginning using file.seek(0).
def get_number_lines(file_dir):
exists = os.path.isfile(file_dir)
if (exists):
print(file_dir)
line_count = 0
read_file = open(file_dir,'r')
read_file.seek(0)
for line_num, line in enumerate(read_file.readlines()):
line_count = line_num
print(line)
read_file.close()
return (line_count + 1)
else:
print("ERROR: FILE \"" + file_dir + "\" does not exist.")
exit()
Strangely, when I try calling the function it runs ok but the output is telling me that my file is 3 lines shorter than it actually is. When I print the file lines it appears to be skipping the last 3 lines of the file and I'm not sure why.
I have tested the below code using "with open" instead of read_file.seek.
Personal opinion but it works a lot better for reading .txt files. The function will return the number of lines found in the path given to the function. If it is not a file that exists it will error and exit.
def Get_Number_Lines(file_dir):
exists = os.path.isfile(file_dir)
if (exists):
print(file_dir)
line_count = 0
with open(file_dir, 'rb') as fin:
reader = fin.readlines()
for line in reader:
line_count += 1
return line_count
else:
print("ERROR: FILE \"" + file_dir + "\" does not exist.")
exit()
Appreciate all the suggestions. So I discovered that I had a file object (in write mode) open prior to calling the get_number_lines() function i.e.
write_file = open(outputFileDir,"w+")
# do stuff
get_number_lines(outputFileDir)
I then tried closing the the file prior to calling the function which solved the issue I was having. Out of curiousity I also tried this, which works no problem:
write_file = open(outputFileDir,"w+")
# do stuff
write_file.close()
read_file.open(outputFileDir,"r")
get_number_lines(outputFileDir)
I didn't realise having two file objects (one in read, and one in write) could cause this issue.
I'm farily new to python and I'm currently stuck when trying to improve my script. I have a script that performs a lot of operations using selenium to automate a manual task. The scripts opens two pages, searches for an email, fetches data from that page and sends it to another tab. I need help to feed the script a textfile containing a list of email addresses, one line at a time and using each line to search the webpage. What I need is the following:
Open file "test.txt"
Read first line in text file and store this value for use in another function.
perform function which uses line from text file as its input value.
Add "Completed" behind the first line in the text file before moving to the next
Move to and read next line i text file, store as variable and repeat from step 3.
I'm not sure how I can do this.
Here is a snippet of my code at the time:
def fetchEmail():
fileName = input("Filename: ")
fileNameExt = fileName + ".txt" #to make sure a .txt extension is used
line = f.readline()
for line in f:
print(line) # <-- How can I store the value here for use later?
break
def performSearch():
emailSearch = driver.find_element_by_id('quicksearchinput')
emailSearch.send_keys(fetchEmail, Keys.RETURN) <--- This is where I want to be able to paste current line for everytime function is called.
return main
I would appreciate any help how I can solve this.
It's a bit tricky to diagnose your particular issue, since you don't actually provide real code. However, probably one of the following will help you:
Return the list of all lines from fetchEmail, then search for all of them in send_keys:
def fetchEmail():
fileName = input("Filename: ")
fileNameExt = fileName + ".txt"
with open(fileNameExt) as f:
return f.read().splitlines()
def performSearch():
emailSearch = driver.find_element_by_id('quicksearchinput')
emailSearch.send_keys(fetchEmail(), Keys.RETURN)
# ...
Yield them one at a time, and look for them individually:
def fetchEmail():
fileName = input("Filename: ")
fileNameExt = fileName + ".txt"
with open(fileNameExt) as f:
for line in f:
yield line.strip()
def performSearch():
emailSearch = driver.find_element_by_id('quicksearchinput')
for email in fetchEmail():
emailSearch.send_keys(email, Keys.RETURN)
# ...
I don't recommend using globals, there should be a better way to share information between functions (such as having both of these in a class instance, or having one function call the other like I show above), but here would be an example of how you could save the value when the first function gets called, and retrieve the results in the second function at an arbitrary later time:
emails = []
def fetchEmail():
global emails
fileName = input("Filename: ")
fileNameExt = fileName + ".txt"
with open(fileNameExt) as f:
emails = f.read().splitlines()
def performSearch():
emailSearch = driver.find_element_by_id('quicksearchinput')
emailSearch.send_keys(emails, Keys.RETURN)
# ...
I want to build a function that asks the user to type in a source filename and a destination filename; opens the files, loops through the contents of the source file line-by-line, writing each one to the destination file; and then closes both files. Make sure that all work with the files happens inside of a try block. If an IOError occurs, the script should print a message saying that there was an error working with one of the files and ask the user to enter in the filenames again. Here is what I have so far:
while True:
try:
file = open(filename)
line = file.readline()
while line != "":
print(line)
line = file.readline()
file.close()
break
except IOError:
print("There was a problem accessing file '" + filename + "'." + \
"Please enter a different filename.")
filename = input("> ")
However, I don't know how to ask the user for 1.) user input 2.) ask for both the filename and destination filename 3.) writing each source to the destination file. Help if you can..
There are some things that I could show you.
To do input
inputFileName = str(raw_input("Enter the input file: "))
outputFileName = str(raw_input("Enter the output file name: "))
Also, to learn about using files you can check out a good tutorial here.
Finally, you shouldn't be running your file = open(filename) in a while loop. Only the reading of the lines should be done in a loop.
You only need to add a little to your existing code:
in_filename = input('input from filename: ')
ou_filename = input('output to filename: ')
while True:
try:
infile = open(in_filename)
oufile = open(ou_filename, 'w')
line = infile.readline()
while line != "":
# print(line)
oufile.write(line)
line = infile.readline()
infile.close()
oufile.close()
break
except IOError:
print("There was a problem accessing file '" + in_filename + "'." + \
"or maybe '" + ou_filename + "'." + \
"Please enter different filenames.")
in_filename = input('input from filename: ')
ou_filename = input('output to filename: ')
Of course, there are many things left to improve here -- for example, by insisting that everything be within a single try/except statement, you don't know, in the error message, which of the two files gave problems.
But at least I've added the in_ and ou_ prefixes to distinguish input from output, and avoided using the built-in name file as a variable name (trampling over built-in names is a notorious trap for beginners, which gives no problems... until it suddenly does:-).
1.2) You can get the user input and both filename by sys.argv sys.argv.
3) File operation. file
I'm trying to write a python program that will read input and copy it to standard output (with no alterations). I've been told that it needs to operate as a Python version of the Unix cat function. If a file cannot be opened, an error message needs to be printed, and then the program needs to continue processing any additional files. I am a complete beginner, and have tried my best to scrape something together with my limited knowledge. Here is what I have so far:
from sys import argv, stdout, stdin, stderr
if len(argv) == 1:
try:
stdout.write(raw_input(' ') + '\n')
except:
stderr.write ('sorry' + '\n')
quit()
else:
for filename in argv[1:]:
try:
filehandle + open(filename)
except IOError:
stderr.write('Sorry, could not open', filename + '\n')
continue
f = filehandle.read()
stdout.write(f)
I am not quite sure where to go from here.. does anyone have any advice/am I on the right track even a little bit? Please and thank you!
This function will copy the specified file to the console line by line (in case you later on decide to give it the ability to use the -n command line option of cat)
def catfile(fn):
with open(fn) as f:
for line in f:
print line,
It can be called with the filename once you have established the file exists.