Categories
Python Answers

How to get the absolute path from relative path in Python?

Sometimes, we want to get the absolute path from relative path in Python.

In this article, we’ll look at how to get the absolute path from relative path in Python.

How to get the absolute path from relative path in Python?

To get the absolute path from relative path in Python, we can use the os.path.join method.

For instance, we write

import os

dirname = os.path.dirname(__file__)
filename = os.path.join(dirname, "relative/path/to/file/you/want")

to get the path of the current script’s directory with

dirname = os.path.dirname(__file__)

Then we call os.path.join to join the current script path with the relative path to return the absolute path.

Conclusion

To get the absolute path from relative path in Python, we can use the os.path.join method.

Categories
Python Answers

How to set the correct encoding when piping stdout in Python?

Sometimes, we want to set the correct encoding when piping stdout in Python.

In this article, we’ll look at how to set the correct encoding when piping stdout in Python.

How to set the correct encoding when piping stdout in Python?

To set the correct encoding when piping stdout in Python, we can set sys.stdout to the right encoding.

For instance, we write

import sys
import codecs

sys.stdout = codecs.getwriter('utf8')(sys.stdout)

to call codecs.getwriter with 'utf8' to set the encoding to utf8.

And then we call the returned function with sys.stdout to set the stdout’s encoding to utf8.

Finally, we assign the returned object to sys.stdout to set the encoding.

Conclusion

To set the correct encoding when piping stdout in Python, we can set sys.stdout to the right encoding.

Categories
Python Answers

How to get JSON to load into an OrderedDict with Python?

Sometimes, we want to get JSON to load into an OrderedDict with Python.

In this article, we’ll look at how to get JSON to load into an OrderedDict with Python.

How to get JSON to load into an OrderedDict with Python?

To get JSON to load into an OrderedDict with Python, we can use the json.loads method.

For instance, we write

import json

data = json.loads('{"foo":1, "bar":2, "fiddle":{"bar":2, "foo":1}}')
print(json.dumps(data, indent=4))

to call json.loads with the JSON string to load the string into an ordered dict.

Then we call json.dumps with data and indent set to 4 to pretty print the JSON with 4 spaces for indentation at each level.

Conclusion

To get JSON to load into an OrderedDict with Python, we can use the json.loads method.

Categories
Python Answers

How to determine the type of an object with Python?

Sometimes, we want to determine the type of an object with Python.

In this article, we’ll look at how to determine the type of an object with Python.

How to determine the type of an object with Python?

To determine the type of an object with Python, we can use the type or isinstance functions.

For instance, we write

type([]) is list

class Test (object):
    pass
a = Test()
type(a) is Test 

to check if [] is a list with

type([]) is list

And we check if a is a Test class instance with

type(a) is Test 

type returns the class of the variable.

We can use isinstance to check if a variable is of a given type.

For instance, we write

isinstance(b, Test)

to check if b is a instance of the Test class. It returns True if b is a Test instance and False otherwise.

Conclusion

To determine the type of an object with Python, we can use the type or isinstance functions.

Categories
Python Answers

How to get the last element of a list with Python?

Sometimes, we want to get the last element of a list with Python.

In this article, we’ll look at how to get the last element of a list with Python.

How to get the last element of a list with Python?

To get the last element of a list with Python, we can use index -1.

For instance, we write

some_list = [1, 2, 3]
last = some_list[-1]

to get the last item from some_list with some_list[-1].

Conclusion

To get the last element of a list with Python, we can use index -1.