Categories
Python Answers

How to send file using POST from a Python script?

Sometimes, we want to send file using POST from a Python script.

In this article, we’ll look at how to send file using POST from a Python script.

How to send file using POST from a Python script?

To send file using POST from a Python script, we call request.post with the files argument.

For instance, we write

with open('report.xls', 'rb') as f:
    r = requests.post('http://httpbin.org/post', files={'report.xls': f})

to open the report.xls file with open.

Then we call requests.post with the URL we want to send the file to via a POST request.

And we set the files argument to a dict with the key for the file and the file f itself.

Conclusion

To send file using POST from a Python script, we call request.post with the files argument.

Categories
Python Answers

How to percent-encode URL parameters in Python?

Sometimes, we want to percent-encode URL parameters in Python.

In this article, we’ll look at how to percent-encode URL parameters in Python.

How to percent-encode URL parameters in Python?

To percent-encode URL parameters in Python, we can use the urllib.parse module.

For instance, we write

import urllib.parse

print(urllib.parse.quote("Müller".encode('utf8')))

to call urllib.parse.quote with the "Müller".encode('utf8')) UTF string to percent encode the string.

We can decode the string with unquote by writing

print(urllib.parse.unquote("M%C3%BCller"))

Conclusion

To percent-encode URL parameters in Python, we can use the urllib.parse module.

Categories
Python Answers

How to get a value of datetime.today() in Python that is time zone aware?

Sometimes, we want to get a value of datetime.today() in Python that is time zone aware.

In this article, we’ll look at how to get a value of datetime.today() in Python that is time zone aware.

How to get a value of datetime.today() in Python that is time zone aware?

To get a value of datetime.today() in Python that is time zone aware, we can use the pytz module.

For instance, we write

import pytz
from datetime import datetime

utc_now = datetime.now(pytz.utc)

to call datetime.now with pytz.utc to get the current datetime in UTC.

Conclusion

To get a value of datetime.today() in Python that is time zone aware, we can use the pytz module.

Categories
Python Answers

How to choose longest string in list with Python?

Sometimes, we want to choose longest string in list with Python.

In this article, we’ll look at how to choose longest string in list with Python.

How to choose longest string in list with Python?

To choose longest string in list with Python, we can use the max function with the key argument set to len.

For instance, we write

my_list = ['123','123456','1234']
print(max(my_list, key=len))

to call max with my_list with key set to len to get the longest string from my_list.

Conclusion

To choose longest string in list with Python, we can use the max function with the key argument set to len.

Categories
Python Answers

How to get the parent directory in Python?

Sometimes, we want to get the parent directory in Python.

In this article, we’ll look at how to get the parent directory in Python.

How to get the parent directory in Python?

To get the parent directory in Python, we can use the pathlib module.

For instance, we write

from pathlib import Path

path = Path("/here/your/path/file.txt")
print(path.parent.absolute())

to create a Path object with the path.

Then we get the parent directory with path.parent.

And we call absolute to get the absolute path of the parent directory.

Conclusion

To get the parent directory in Python, we can use the pathlib module.