Categories
Python Answers

How to create an empty list in Python with certain size?

To create an empty list in Python with certain size, we can use the * operator.

For instance, we write:

l = [None] * 10
print(l)

We create a list with 10 None entries with [None] * 10.

Therefore, l is [None, None, None, None, None, None, None, None, None, None].

Categories
Python Answers

How to get the full path of the current file’s directory with Python?

Sometimes, we want to get the full path of the current file’s directory with Python.

In this article, we’ll look at how to get the full path of the current file’s directory with Python.

How to get the full path of the current file’s directory with Python?

To get the full path of the current file’s directory with Python, we can use the pathlib module.

To get the directory of the script being run, we write:

import pathlib

p = pathlib.Path(__file__).parent.resolve()
print(p)

Then p is something like '/home/runner/ShockingRosyGui'.

To get the current working directory, we write:

import pathlib

p = pathlib.Path().resolve()
print(p)

We call resolve on the Path instance to return the current working directory.

Conclusion

To get the full path of the current file’s directory with Python, we can use the pathlib module.

Categories
Python Answers

How to find an item in a list in Python?

Sometimes, we want to find an item in a list in Python.

In this article, we’ll look at how to find an item in a list in Python.

How to find an item in a list in Python?

To find an item in a list in Python, we can use list comprehension.

For instance, we write:

lst = [1, 2, 3, 4, 5, 6, 7, 8, 9]
matches = [x for x in lst if x > 6]
print(matches)

to return all the entries in lst that are bigger than 6.

We specify the condition of the items to include with the returned list with if x > 6.

And we specify the list to filter with x for x in lst.

Therefore, matches is [7, 8, 9].

Conclusion

To find an item in a list in Python, we can use list comprehension.

Categories
Python Answers

How to format floats without trailing zeros with Python?

Sometimes, we want to format floats without trailing zeros with Python.

In this article, we’ll look at how to format floats without trailing zeros with Python.

How to format floats without trailing zeros with Python?

To format floats without trailing zeros with Python, we can use the rstrip method.

For instance, we write:

x = 3.14000
n = ('%f' % x).rstrip('0').rstrip('.')
print(n)

We interpolate x into a string and then call rstrip with 0 and '.' to remove trailing zeroes from the number strings.

Therefore, n is 3.14.

Conclusion

To format floats without trailing zeros with Python, we can use the rstrip method.

Categories
Python Answers

How to tail a log file in Python?

Sometimes, we want to tail a log file in Python.

In this article, we’ll look at how to tail a log file in Python.

How to tail a log file in Python?

To tail a log file in Python, we can run tail with the sh module.

For instance, we write:

from sh import tail

for line in tail("-f", "foo.txt", _iter=True):
    print(line)

We call tail with the file name and _iter set to True to let us run an infinite loop and print out line which has the output.

Conclusion

To tail a log file in Python, we can run tail with the sh module.