This question already has answers here:
How to check whether a file is empty or not
(11 answers)
Closed 6 years ago.
Is there a way to check if a text file is empty in Python WITHOUT using os ?
What I have tried so far
x = open("friends.txt")
friendsfile = x.readlines()
if friendsfile == None
But I don't think it's the correct way.
Not sure why you wouldn't use os, but I suppose if you really wanted you could open the file and get the first character.
with open('friends.txt') as friendsfile:
first = friendsfile.read(1)
if not first:
print('friendsfile is empty')
else:
#do something
Related
This question already has answers here:
How to get last items of a list in Python?
(5 answers)
Closed 10 months ago.
From the log file I need the last lines. I listed them in [-1:-7]
f_read = open("C:\logs.txt", "r")
status = f_read.readlines()[-1:-7]
print(status)
Python outputs
[]
How can I make it show strings?
[-7:-1]
it must be from lower to upper
This question already has answers here:
Printing a function in Python
(5 answers)
Closed 3 years ago.
I'm trying to write program, who's printout specific words (without "e"). But i have a problems.
that's my code:
def has_no_e(fin,word):
fin = open('words.txt')
for line in fin:
word = line.strip()
if 'e' not in word:
print(word)
else:
continue
print(has_no_e)
Pycharm after run it printout that:
function has_no_e at 0x00E078A0
I don't know what's wrong. Thanks everybody for any help.
Try has_no_e(<fin>, <word>), without the print. What you're doing there is printing the function address itself, if you try doing has_no_e(<fin>, <word>) the function contents will be executed instead, which is what you want.
P.S. Replace fin and word with the actual parameters you want to pass.
This question already has answers here:
How to delete a character from a string using Python
(17 answers)
Closed 4 years ago.
I'm making an odoo10 module, but I need to change a string like
000-000
to
000000
How can I do that?
print("000-000".replace("-", "")) #Output: "000000"
Try this
Oldstr = “000-000”
Newstr = Oldstr.replace(“-”,””)
That should do it.
What I did:
Make the string “000-000”
Made a new string and replaced the “- ”in the old one with nothing and saved it in a new variable
This question already has answers here:
Search and replace operation
(2 answers)
Closed 6 years ago.
I have written code to search and replace string in make command as per user input
make language='english' id=234 version=V1 path='/bin'
In above code i searched version=V1 and replace version with version=V2
import re
strings = "make language='english' id=234 version=V1 path='/bin'"
search_pattern= re.search('version=(.*?)\s', strings)
old_str = search_pattern.group(1)
print test.replace(old_str, "V2")
Can anyone help me write above code in pythonic way or any other way to write above code
It's very easy if you use str.replace
String = "make language='english' id=234 version=V1 path='/bin'"
String = String.replace("version=V1", "version=V2")
This question already has answers here:
Understanding slicing
(38 answers)
Closed 9 years ago.
I am new to python and trying to understand the _full_path from this example.
def _full_path(self, partial):
if partial.startswith("/"):
partial = partial[1:]
path = os.path.join(self.root, partial)
return path
What does the function do? Specifically, what does this line do?
partial = partial[1:]
It seems like some kind of list manipulation -- but I can't find syntax like that in this document.
What is the root property of self that is getting called?
Can somebody explain a little bit about what is happening in that code.
Because os.path.join will take later path start with '/' as base, try this:
print os.path.join('/a', '/b/')
it return '/b/', so you have to check and remove begin slash when you join path.
str is a sequence type, check here: http://docs.python.org/2/library/stdtypes.html#sequence-types-str-unicode-list-tuple-bytearray-buffer-xrange
That line drops the starting "/".
The function itself gives back the "full path".