if statement in one-line for loop python
>>> [(i) for i in my_list if i=="two"]
['two']
if statement in one-line for loop python
>>> [(i) for i in my_list if i=="two"]
['two']
with statement in python
In python if you want to operate some file then you have to use some specific function for that after using that we can read or manipulates the data of the file.
And for operating the file we use open() function. open returns the file object, from which we can access functions and attributes for performing file operation by opening the file.
In the normal file operation we have to follow some rules, like opening the file using 'open()' method then read the file data using 'read()' method after that print the data of file and when all operation gets over we need to close the file using 'close()' method.
Example:
file = open('abc.txt')
data = file.read()
print data
file.close() #file closing is must or it will throw error
Using with statement we can get automatic exception handelling and better syntax.
at the end there is no need of write closing of file it automatically get space cleared by with statement.
Example:
with open('abc.txt') as file: # refer file as object for file object
data = file.read()
print data
or
file.write('Hello python')
you can check we not included file close method in the with statement program.
Done!!!
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us