Overview for python

How to find the smallest file in a directory in Python

Sometime while creating a program or writing a script in Python we need to find the largest file in a current directory. This is very simple task and can done by running a following script:

import os

files = os.listdir(".")

smallest_file_size = 10000
smallest_file = ""

for file in files:
    if os.path.isfile(file):
        size = os.path.getsize(file)
        if size <  smallest_file_size:
            smallest_file_size = size
            smallest_file = file

print("the smallest file in current directory has filename "+smallest_file+ " and size: "+str(smallest_file_size)+" bytes.")

What does that code?

First, we are making a list of files in a current directory using os module. In order to understand, what does further code, we have to check, what kind of object we get by running os.listdir() command.

Read more

Written by Administrator on Sunday May 17, 2020

How to find the largest file in a directory in Python

Sometime while creating a program or writing a script in Python we need to find the largest file in a current directory. This is very simple task and can done by running a following script:

import os

files = os.listdir(".")

largest_file_size = 0
largest_file = ""

for file in files:
    if os.path.isfile(file):
        size = os.path.getsize(file)
            if size <  largest_file_size:
                largest_file_size = size
                largest_file = file

print("largest file in current directory has filename "+largest_file+ " and size: "+str(largest_file_size)+" bytes.")

Output in my case:

dima@linux-zsrx:~/python> ls -l
total 24
-rw-r--r-- 1 dima users 2036 May 17 16:29 dics.py
-rw-r--r-- 1 dima users  405 May 18 03:40 largest_file_in_directory.py
-rw-r--r-- 1 dima users  109 May 17 17:06 levyi
-rw-r--r-- 1 dima users  680 May 17 19:00 levyi.py
-rw-r--r-- 1 dima users  125 May 17 19:33 test01.py
-rw-r--r-- 1 dima users  241 May 18 00:33 translitgeo.py
dima@linux-zsrx:~/python> python smallest_file_in_directory.py 
the smallest file in current directory has filename levyi and size: 109 bytes.

What does that code?

First, we are making a list of files in a current directory using os module. In order to understand, what does further code, we have to check, what kind of object we get by running os.listdir() command.

Normally we have to check, if chosen item is directory or file, but we know, that directory link cannot be the biggest item, that's why we dont't have to check it.

Read more

Written by Administrator on Sunday May 17, 2020

Print current directory path in Python

In order to print current directory path in Python (meaning, directory, where a python script is located), you have to import os module and then run a following script:

dir_path = os.path.dirname(os.path.realpath(__file__))
print(dir_path)

os.path.dirname is defining the current path, where your python script is stored and second line prints it into command line (terminal).

Output:

dima@linux-zsrx:~/python> python files.py
/home/dima/python
dima@linux-zsrx:~/python> 

Read more

Written by Administrator on Sunday May 17, 2020

How to install Python 3.8 from command line in LiteLinux

First, let's check, if python was already installed in our system. Use following command:

$ python -V

Ok, now we know, that python version 2.7.15+ is already installed. But we want to install python 3.8, because for some reasons python 2.7 we don't need. Let's go:

$ sudo apt install python3.8

You will see something like this:

So, python 3.8 is installed. We can check it:

$ python3.8 --version

Read more

Written by Administrator on Tuesday March 10, 2020

How to Check Python Version in CentOS Command Line

If you have CentOS system and want to know, if python is installed or not, it's enough to ask system, what version of python is installed. There's very simple command:

$ python -V

You will receive an answer:

or:

version of python in linux

or 

installed python in linux version

Read more

Written by Administrator on Saturday March 7, 2020