Categories
Python Answers

How to get a directory listing sorted by creation date in Python?

Sometimes, we want to get a directory listing sorted by creation date in Python.

In this article, we’ll look at how to get a directory listing sorted by creation date in Python.

How to get a directory listing sorted by creation date in Python?

To get a directory listing sorted by creation date in Python, we can use the pathlib module.

For instance, we write:

import os
from pathlib import Path

dirpath = '/'
paths = sorted(Path(dirpath).iterdir(), key=os.path.getmtime)
print(paths)

We get the directories within dirpath with Path(dirpath).iterdir().

And we call sorted with Path(dirpath).iterdir() and key to os.path.getmtime sort by time modified.

Finally, we assign the returned list of path objects to paths.

Therefore, paths is something like:

[PosixPath('/boot'), PosixPath('/media'), PosixPath('/srv'), PosixPath('/lib64'), PosixPath('/var'), PosixPath('/home'), PosixPath('/root'), PosixPath('/sbin'), PosixPath('/lib32'), PosixPath('/gocode'), PosixPath('/run_dir'), PosixPath('/phase2-erlang.tar.bz2'), PosixPath('/phase2-jest.tar.bz2'), PosixPath('/phase2-d.tar.bz2'), PosixPath('/phase2-mercury.tar.bz2'), PosixPath('/phase2-express.tar.bz2'), PosixPath('/phase2-php.tar.bz2'), PosixPath('/phase2-guile.tar.bz2'), PosixPath('/phase2-csharp.tar.bz2'), PosixPath('/phase2-fortran.tar.bz2'), PosixPath('/phase2-fsharp.tar.bz2'), PosixPath('/phase2-rlang.tar.bz2'), PosixPath('/phase2-cpp.tar.bz2'), PosixPath('/phase2-assembly.tar.bz2'), PosixPath('/phase2-crystal.tar.bz2'), PosixPath('/phase2-pascal.tar.bz2'), PosixPath('/phase2-prolog.tar.bz2'), PosixPath('/phase2-haskell.tar.bz2'), PosixPath('/phase2-clisp.tar.bz2'), PosixPath('/phase2-love2d.tar.bz2'), PosixPath('/phase2-elixir.tar.bz2'), PosixPath('/phase2-cpp11.tar.bz2'), PosixPath('/phase2-react_native.tar.bz2'), PosixPath('/bin'), PosixPath('/lib'), PosixPath('/usr'), PosixPath('/opt'), PosixPath('/mnt'), PosixPath('/inject'), PosixPath('/io'), PosixPath('/etc'), PosixPath('/.dockerenv'), PosixPath('/sys'), PosixPath('/proc'), PosixPath('/dev'), PosixPath('/config'), PosixPath('/nix'), PosixPath('/run'), PosixPath('/tmp')]

Conclusion

To get a directory listing sorted by creation date in Python, we can use the pathlib module.

Categories
Python Answers

How to change the user agent header with Python urllib?

Sometimes, we want to change the user agent header with Python urllib.

In this article, we’ll look at how to change the user agent header with Python urllib.

How to change the user agent header with Python urllib?

To change the user agent header with Python urllib, we can call the build_opener method.

Then we set the addheaders attribute of the returned object to add the user-agent request header.

For instance, we write:

import urllib.request

opener = urllib.request.build_opener()
opener.addheaders = [('User-Agent', 'Mozilla/5.0')]
response = opener.open('http://www.example.com')
print(response)

We call urllib.request.build_opener method and assign the returned object to opener.

Then we set opener.addheaders attribute to [('User-Agent', 'Mozilla/5.0')] to set the user-agent header to Mozilla/5.0.

Next, we call opener.open with the URL we want to make a GET request to and assign the returned response to response.

Finally, we print the response.

Conclusion

To change the user agent header with Python urllib, we can call the build_opener method.

Then we set the addheaders attribute of the returned object to add the user-agent request header.

Categories
Python Answers

How to do currency formatting in Python?

Sometimes, we want to do currency formatting in Python.

In this article, we’ll look at how to do currency formatting in Python.

How to do currency formatting in Python?

To do currency formatting in Python, we can use the locale.setlocale method.

For instance, we write:

import locale

locale.setlocale(locale.LC_ALL, '')
c = locale.currency(188518982.18, grouping=True)
print(c)

We call locale.setlocale to set the locale of the script by getting the locale settings from the operating system by setting it to locale.LC_ALL.

Then we call locale.currency with the float to format into a currency string.

And we set grouping to True to group the digits with the digits separator according to the locale.

Therefore, c is $188,518,982.18 given that the OS’ locale is 'English_United States.1252'.

Conclusion

To do currency formatting in Python, we can use the locale.setlocale method.

Categories
Python Answers

How to decode a UTF-8 URL string in Python?

Sometimes, we want to decode a UTF-8 URL string in Python.

In this article, we’ll look at how to decode a UTF-8 URL string in Python.

How to decode a UTF-8 URL string in Python?

To decode a UTF-8 URL string in Python, we can use the unquote function from the urllib.parse module.

For instance, we write:

from urllib.parse import unquote

url = 'example.com?title=%D0%BF%D1%80%D0%B0%D0%B2%D0%BE%D0%B2%D0%B0%D1%8F+%D0%B7%D0%B0%D1%89%D0%B8%D1%82%D0%B0'
decoded = unquote(url)
print(decoded)

to decode the url with unquote and assign the decoded URL string to decoded.

Therefore, decoded is 'example.com?title=правовая+защита'.

Conclusion

To decode a UTF-8 URL string in Python, we can use the unquote function from the urllib.parse module.

Categories
Python Answers

How to check if a word is in a string with Python?

Sometimes, we want to check if a word is in a string with Python.

In this article, we’ll look at how to check if a word is in a string with Python.

How to check if a word is in a string with Python?

To check if a word is in a string with Python, we can use the in operator.

For instance, we write:

if 'seek' in 'those who seek shall find':
    print('Success!')

to check if 'seek' is included in 'those who seek shall find'.

Since this is True, 'Success!' is printed.

Conclusion

To check if a word is in a string with Python, we can use the in operator.