filesystem — A module that contain some helpful function to deal with filesystem.

NIWLittleUtils.filesystem.scantree(path='.')[source]

Recursively yield DirEntry objects for given directory.

Python 3.5 and higher version has os.scandir() to get the files and folders and their file attribute information in folder given by path. But Python version won’t scandir recursively, this function can scan folder recursively.

Notice that this function will not follow symlinks to avoid symlink loops.

Parameters:path – Path to folder which you want to scan.
NIWLittleUtils.filesystem.secure_filename(filename, codec='utf8')[source]

Pass it a filename and it will return a secure version of it.

This finction is a modified version of werkzeug.utils.secure_filename.

The filename can then safely be stored on a regular file system and passed to os.path.join().

You can use parameter codec to specify the codec which is used to encode the filename. The codec could only be utf8 or ascii. If you need high portability, you should let codec to be 'ascii'. It will be 'utf8' by default.

On windows systems the function also makes sure that the file is not named after one of the special device files.

>>> secure_filename("My cool movie.mov")
'My_cool_movie.mov'
>>> secure_filename("../../../etc/passwd")
'etc_passwd'
>>> secure_filename('i contain cool \xfcml\xe4uts.txt')
'i_contain_cool_ümläuts.txt'
>>> secure_filename('i contain cool \xfcml\xe4uts.txt', 'ascii')
'i_contain_cool_umlauts.txt'

The function might return an empty filename. It’s your responsibility to ensure that the filename is unique and that you generate random filename if the function returned an empty one.

User should remember, this function will make sure filename is secure, but it cannot make sure file itself is secure. For example, if a user upload a file named test.sh, this function will return same filename. If this file has security vulnerabilities script inside and you execute it… well, good luck.

Parameters:filename – the filename to secure