I'm trying to reverse the sentence and write it in another file but when I run it, it creates an empty reversed.txt file.
The following two lines are in file input:
Hello World
How is everyone doing?
The file output will have the lines:
dlroW olleH
?gniod enoyreve si woH
My code:
#!/usr/local/bin/python
name = raw_input("Enter name of file: ")
def Reverse(name):
try:
input_file=open(name,"r")
reversed = "reversed.txt"
output_file=open(reversed,"w")
list=input_file.readlines()
for i in range(0,len(list)):
d = int(len(list) - i)
output_file.write(list[d])
except IOError:
print("Cannot open file")
except:
print("Other errors")
else:
print("success")
finally:
print("prints always")
input_file.close()
output_file.close()
#returns reversed
Reverse(name)
When I run this it prints "Other errors". It creates the new file reversed.txt but it`s empty.
There are a couple of things wrong with your code snippet. But this should work better:
#!/usr/local/bin/python
name = raw_input("Enter name of file: ")
def Reverse(name):
try:
input_file=open(name,"r")
reversed = "reversed.txt"
output_file=open(reversed,"w")
for line in input_file.readlines():
output_file.write(line[-2::-1] + "\n")
except IOError:
print("Cannot open file")
except:
print("Other errors")
else:
print("success")
finally:
print("prints always")
input_file.close()
output_file.close()
#returns reversed
Reverse(name)
Here we iterate over the read lines and then use the fact that strings can be viewed as arrays to reverse each line.
EDIT
By using line[-2::-1] we avoid having the ending new line at the start of the line we write to the file. Then we append the "\n" so that each line is properly separated by the new line character.
You can try something like this:
lines=input_file.readlines()
for line in lines:
line = ''.join(list(reversed(line)))
output_file.write(line)
If I were to tell you what's wrong, without improving your implementation (which is definitely possible ;) ) I'd like to suggest a few things:
Indents are super important:
In your code just after the def you have a blank line. This means your function ends before it starts, and your try excepts are just 'floating'.
Your excepts aren't very helpful: You just print small information when catching exceptions. You have no idea why the exception is thrown. A better way to do this is print the exception with your own custom message. Something like:
try:
someCode()
except IOError as ioE:
print "Can't open file! - ", ioE # Change to print() for py3.x
except Exception as otherE:
print "Unknown error! - ", otherE
Initialize variables you intend to use: If you are using a variable, define it! Make sure any flow of loops/try-excepts will end up initializing your vairables. When in doubt, initialize as None (and check for None when using it). In your code, input_file is being used in finally. But if the input_file=open(name,"r") raises an exception, input_file will remain uninitialized causes another exception in finally.
Other than that your implementation can be improved from the other answers.
Hope this helps!
Related
I was creating a note taking program, using the user voice command. When the file already exists, I want to show an error. It runs smoothly when the file name is different though.
When the file already exists, I have coded to open in the writing mode. It would erase the earlier file, and write new instead of adding in the append mode.
elif 'note' in query:
try:
speak(" Okay master. what's the file name")
b= takecommand()
f = open(f"{b}.txt","w")
speak("Okay master. tell me what to note down")
a= takecommand()
f.write(f"{a}\n")
f.close()
except Exception as e:
speak("file name already exist")
Can you help me with troubleshooting the script? Like how could I first make it thrown an error when the filename is same?
You'd want to check if the file exists after they input it, which can be done using "os.path.exists" as also suggested by Tim Roberts. Here's code that should work for you:
elif 'note' in query:
try:
Looper = True # Added a loop so it can repeatedly ask the user until they give a valid answer.
speak(" Okay master. what's the file name") # Put here instead of in the loop so it will repeat once since the else condition already asks for a new file name aswell.
while Looper == True:
b = takecommand()
if os.path.exists(f"{b}.txt"):
f = open(f"{b}.txt","w")
speak("Okay master. tell me what to note down")
a= takecommand()
f.write(f"{a}\n")
Looper = False # So the loop won't repeat again (regardless of the below line).
f.close()
else:
speak("That file already exists. Give me a new file name.")
except Exception as e:
speak("Hm, an error occured. Try again later.")
I also added in a while loop for you, so that way if the user gives a file name that already exists, it will keep asking until they give a valid file name.
Ensure you have OS imported for the code to work by adding the following to the top of your code:
import os
Use mode 'x'
x' create a new file and open it for writing. The 'x' mode implies 'w'
and raises an FileExistsError if the file already exists.
try:
# ...
f = open(f"{b}.txt","x")
# ...
except FileExistsError:
speak("file name already exist")
Or mode 'a' to append the strings to existing file.
'a' open for writing, appending to the end of the file if it exists
I see that this seems to be a common error, but I'm not seeing the answer for my case.
UnboundLocalError: local variable 'tfstate_dict' referenced before assignment
#!/usr/bin/env python
import json
def main():
get_sfleet_id()
def get_sfleet_id():
try:
f=open("terraform_remote.tfstate", "r")
contents =f.read()
tfstate_dict = json.load(contents)
except:
print("error loading %s" % f)
print(contents)
print(tfstate_dict)
if __name__ == '__main__': main()
As I wrote in my comment, tfstate_dict does not come in to existence until you exit your try block. But that's not to say that it applies to all the preceding code; it simply applies to tfstate_dict because it happens to be the very last line.
This is easily testable with the following:
try:
a = int(2)
b = int(3)
c = int('hi')
except:
print(locals())
print()
print(locals().get('a'))
You should see that 'a' and 'b' are both defined and can be accessed (depending on how you're running this code, there could a lot of stuff in locals() too). So, the existence of 'a' and 'b' gives you no assurance that 'c' exists.
There's two issues with your current exception handling:
There's probably too much going on in your try block to be handled the way you currently do. This code will fail if the file cannot be located, but you wouldn't necessarily know that was happening. And if your code originally failed only on tfstate_dict = json.load(contents) you're now scratching your head why you're getting a NameError on print(contents) all of a sudden.
You don't want to be catching these issues with blanket except. At a minimum you'll want to use except Exception as e:, which allows you to print e too.
Here's a hypothetical situation where you handle the file not existing, and you also give a shot at parsing JSON.
import json
from json.decoder import JSONDecodeError
try:
with open('something.json') as infile:
try:
#data = json.load(infile) # This is what you'd really use
data = json.loads("{hi: 2}") # But let's make it fail
except JSONDecodeError:
print("Not valid JSON, try something else")
data = infile.read()
except FileNotFoundError:
print("Can't find file")
data = ''
I have attempted to get this code working in Python 3.5.1 but I am having problems with it not writing the hashed password and username to the file specified. Is there something I'm doing wrong?
import sys
import hashlib
import getpass
def main():
print ('\nUser & Password Storage Program v.01\n')
if input('The file will be modieifed if it exists.\nDo you wish to continue (Y/N): ') not in ('Y','y'):
sys.exit('\nChanges were not recorded\n')
user_name = input(str('Please Enter a User Name: '))
pwdinput = input("Now enter a password:").encode('utf-8')
password = hashlib.sha224(pwdinput).hexdigest()
try:
f = open(passfile.txt,'a+')
f.write(user_name + '\n')
f.write(password + '\n')
f.close()
except:
sys.exit('There was a problem writing to the file!')
print ('\nPassword safely stored in passfile.txt.\n')
main()
You don't have a variable called passfile.txt, and that is making the open call fail. It would be correctly opening the file if you surrounded it in quotes, like 'passfile.txt'
The reason this syntax error isn't obvious is because you have a catch all, and your program will just print 'There was a problem writing to the file!' instead.
Rather than catch all exceptions using except:, use the syntax except IOError:, as that will be thrown whenever there is an actual error in writing to the file.
The error is that your open() function is taking an object as a parameter, but you probably meant to use it as a string.
Also, you generally should use a with block when opening files.
try:
with open("passfile.txt",'a+') as f:
f.write("{}\n{}\n".format(user_name, password))
except Exception as e: # Use other exceptions for more accurate results
sys.exit('There was a problem writing to the file!\nError: {}'.format(str(e)))
So i'm making a little app here, and I have try blocks (because I need to see if a file already exists or should be created). Although... my try block is repeating for some reason! I have ABSOLUTELY no idea why this is happening. Please help?
Also, the file is created fine :)
Code:
import sys
import time
Version = "V0.1"
def user():
PISBNdat = open("PISBN.dat", "w")
PISBNdat.write(Version)
cuser = raw_input("Please enter account username!")
for line in PISBNdat:
print "Test"
if cuser in line:
print("User already exists! Try again!")
user()
def start():
print "Hello and welcome to Plaz's PISBN!"
print "Opening file..."
time.sleep(0.8)
try:
fin = open("PISBN.dat", "r")
print "Success!"
fin.close()
user()
except:
time.sleep(0.5)
print "Did not recognize/find file!"
time.sleep(0.1)
print "Creating file!"
time.sleep(0.5)
try:
fout = open("PISBN.dat", "w")
print "Success!"
fout.close()
user()
except:
print "Failed!"
exit()
start()
And here's the output...:
Hello and welcome to Plaz's PISBN!
Opening file...
Did not recognize/find file!
Creating file!
Success!
Please enter account username! [This is what I entered: Plazmotech]
Failed!
Now obviously, since it said 'Failed!', it means its running my try block... because thats the only place it could output 'Failed!' So please help here!
Catch only the exceptions you want to handle. Note that printing "Failed!" and exiting is not handling an exception. Python's going to do that anyway, plus it's going to give you a buttload of information as to what happened, so why write extra code that does less and hides the cause of the problem?
As somebody (who just deleted his post) pointed out before, you call user() again in your user function, which is likely by mistake here.
I however believe your problems lie somewhere else. I assume you want "PISBN.dat" to contain a database where you look up for accounts. However, opening the file with only write permission will not help there. This causes your loop "for line in PISBNdat:" not working at all, so that the message "Test" did not appear.
It makes me think that "raw_input" failed and exception is caught. But as kindall pointed out, your code has some design flaws.
Here's an example of start() with correct try...except usage:
def start():
print "Hello and welcome to Plaz's PISBN!"
print "Opening file..."
time.sleep(0.8)
try: #try bloc contains minimum amount of code to catch the pertinent error.
f = open("PISBN.dat", "r")
print "Success!"
except IOError: #Only catch the exceptions you want to handle. IOError, in this case
f = None
if not f:
print "Did not recognize/find file!"
print "Creating file!"
try:
f = open("PISBN.dat", "w")
print "Success!"
except IOError:
print "Failed!"
exit()
f.close()
user() #Call user() after the file has been tested and/or created.
I am having trouble getting this to work correctly (obviously) - I am ALMOST there, and I have a good idea of WHY it is not working - just not sure how to make it work.
This is suppose to attempt to read a file into memory, if it fails it goes to the "except" clause of the block of code (that part is 'duh'). The error file prints: "<main.DebugOutput instance at 0x04021EB8>". What I want it to do is print the actual Error. Like a FileIOError or TraceBackError or whatever it is to that error file. This is just the beginning stages and I plan to add things like date stamps and to have it append, not write/create - I just need the actual error printed to the file. Advice?
import os, sys
try:
myPidFile = "Zeznadata.txt"
myOpenPID_File = open(myPidFile, "r") #Attempts to open the file
print "Sucessfully opened the file: \"" + myPidFile + "\"."
except:
print "This file, \"" + myPidFile + "\", does not exist. Please check the file name and try again. "
myFileErr = open("PIDErrorlog.txt", "w")
myStdError = str(sys.stderr)
myFileErr.write(myStdError)
myFileErr.close()
print "\nThis error was logged in the file (and stored in the directory): "
First, you should use a logging library. It will help you deal with different logging levels (info/warn/error), timestamps and more.
http://docs.python.org/library/logging.html
Second, you need to catch the error, and then you can log details about it. This example comes from the Python documentation.
import sys
try:
f = open('myfile.txt')
s = f.readline()
i = int(s.strip())
except IOError as (errno, strerror):
print "I/O error({0}): {1}".format(errno, strerror)
except ValueError:
print "Could not convert data to an integer."
except:
print "Unexpected error:", sys.exc_info()[0]
raise
See how it catches an IOError and assigns the error number and error message to variables? You can have an except block for each type of error you want to deal with. In the last (generic error) block, it uses sys.exc_info()[0] to get the error details.
http://docs.python.org/tutorial/errors.html
the problem is here:
myStdError = str(sys.stderr)
myFileErr.write(myStdError)
sys.stderr is a file-like interface defined in POSIX standards, called standard error, not "error text" that you can write to a file. So what you (probably) wanted to do is:
sys.stderr = myFileErr
This is the python equivalent to python your_python_sctipt.py 2> PIDErrorLog.txt in unix shell.
Use the logging module. The logging module have exception formatters that will help you print exceptions in a pretty way.
http://docs.python.org/library/logging.html
Like Kimvais says, your problem is:
myStdError = str(sys.stderr)
myFileErr.write(myStdError)
sys.stderr is a file handle to stderr (stdout is what print writes to).
You should do something like:
try:
...open file...
except IOError as (errno, strerror):
...open errfile...
errfile.write(strerror)
errfile.close()
Alison's answer is also very good and well written.