Getting the hostname of the node running a program can be useful in various scenarios, such as creating logs with the hostname, identifying which node a script is running on, or configuring a distributed system with different nodes. In Bash, there are several ways to retrieve the hostname of the machine, as mentioned in the
Read more
Tag: Python
How to get the hostname of the node in Python?
Posted onIn Python, how to get the hostname of the node? In Python, you can get the hostname by the socket.gethostname() library function in the socket module: import socket hostname = socket.gethostname() Example: $ python Python 2.7.5 (default, Nov 6 2016, 00:28:07) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2 Type “help”, “copyright”, “credits” or “license”
Read more
How to convert epoch timestamp to human readable date format in Python?
Posted onHow to convert an epoch timestamp to a human readable date format? In Python: import time time.strftime(“%a, %d %b %Y %H:%M:%S %Z”, time.localtime(epoch)) One example: $ python Python 2.7.5 (default, Nov 6 2016, 00:28:07) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2 Type “help”, “copyright”, “credits” or “license” for more information. >>> import time >>>
Read more
How to get the epoch timestamp in Python?
Posted onIn Python, how to get the epoch time (the number of seconds since epoch)? In Python, you can get the epoch time by calling time.time() which return a floating number: import time print time.time() If you would like to get only the number of seconds, you may convert it to an integer. One example is
Read more
How to get an environment variable in Python?
Posted onIn Python, how to get the value (string) of an environment variable? In Python, all environment variables can be accessed directly through os.environ import os print os.environ[‘HOME’] If the environment variable is not present, it will raise a KeyError. You can use get to return None if the environment variable is not present: print os.environ.get(‘ENV_MIGHT_EXIST’)
Read more
Any good Java REPL tool/implementation?
Posted onAny good suggestions on a Java REPL implementation like ‘scala’ and ‘python’ or ‘php -a’? The java-repl tool https://github.com/albertlatacz/java-repl/ works nicely for most situations for me. It is released as a .jar. Hence, it is easy to download and run: $ wget –quiet https://github.com/albertlatacz/java-repl/releases/download/428/javarepl-428.jar -O /tmp/javarepo-428.jar && java -jar /tmp/javarepo-428.jar One usage example is as
Read more
How to get hostname in Python on Linux?
Posted onIn Python, how to get hostname as the command hostname does on Linux? In Python, you can get the hostname by the socket.gethostname() library function in the socket module: import socket hostname = socket.gethostname() Reference: https://www.systutorials.com/dtivl/20/how-to-get-the-hostname-of-the-node?show=34#a34
How to process a file line by line in Python?
Posted onIn Python, how to process read a file line by line to process it? Like those lines in Bash: while read line ; do echo $line done < ./input.txt In Python, you can process a file line by line by a for in the file like with open(“./input.txt”, “r”) as thefile: for line in thefile:
Read more
How to get an environment variable in Python?
Posted onIn Python, how to get an environment variable? In Python, you may use this piece of code to get an environment variable: os.environ.get(‘ENV_MIGHT_EXIST’) or this piece of code: os.getenv(‘ENV_MIGHT_EXIST’) It will return None if the environment variable is not present. Reference and for more ways, please check https://www.systutorials.com/dtivl/13/how-to-get-an-environment-variable?show=80#answer-80 .
how to get the size of python file without using the os namespace
Posted oni need to create a program which is taking information from printer , so that i cannot use os namespace , so there is any way without using the os namespace You may use a way as follows. open the file seek to the end tell its position which is the file size An example
Read more
How to detect whether a file is readable and writable in Python?
Posted onBefore reading or writing a file, access should be checked first. How to detect whether a file is readable and writable in Python? You can use the os.access(path, mode) library function https://docs.python.org/release/2.6.6/library/os.html#os.access like the Linux access library function for C. It returns True if access is allowed, False if not. For readable and writable, you
Read more
In Python, how to check whether a key already exists in a dict?
Posted onHow to check whether a key already exists in a dict in Python since version 2.6? If d is a dict(), you can directly test whether a key k is in d in Python by the in keyword like if k in d: ..
How to test a file or directory exists in Python?
Posted onIn Bash, the [ -f ] and [ -d ] tests can test whether a file or a directory exist. What are the corresponding python method for these tests? For -f: **os.path.isfile(path)** Return True if path is an existing regular file. This follows symbolic links. For -d: **os.path.isdir(path)** Return True if path is an existing
Read more
How to get file size in Python?
Posted onHow to get a file’s size in Python from the full path of a file? You can use this piece of Python code: os.path.getsize(file_path) It returns the size in bytes. It raises os.error if the file does not exist or is inaccessible. Manual of os.path.getsize(): https://docs.python.org/2/library/os.path.html#os.path.getsize
How to iterate all files and directories under a directory in Python?
Posted onHow to iterate all files and directories under a directory in Python? For example, I would like to iterate each file and directory under /mnt/data/ /mnt/data/ |– file.txt `– great That is: /mnt/data/files.txt /mnt/data/great You can use the os.walk to do this: root, dirs, files = os.walk(path).next() root will be the path.dirs will contain the
Read more
How to get the file extension from a filename in Python?
Posted onHow to get the file extension from a filename in Python? For example, I would like to get “.txt” from “file.txt”. The Python code: >>> name, ext = os.path.splitext(‘file.txt’) >>> name ‘file’ >>> ext ‘.txt’ Manual of os.path.splitext: https://docs.python.org/2/library/os.path.html#os.path.splitext
Required packages for building YouCompleteMe for Vim on Fedora 21
Posted onYouCompleteMe for Vim on Fedora 21 reports this error: [zma@laptop:~/.vim/bundle/YouCompleteMe]$ ./install.sh — The C compiler identification is GNU 4.9.2 — The CXX compiler identification is GNU 4.9.2 — Check for working C compiler: /usr/bin/cc — Check for working C compiler: /usr/bin/cc — works — Detecting C compiler ABI info — Detecting C compiler ABI info
Read more
How to install latest version of Calibre?
Posted onHow to install latest version of Calibre? The version from my distro (Ubuntu, Linux Mint, Fedora) seem at 1.xx while the latest Calibre is already at 2.x. You may check Caibre website’s instruction: http://calibre-ebook.com/download_linux sudo -v && wget -nv -O- https://raw.githubusercontent.com/kovidgoyal/calibre/master/setup/linux-installer.py | sudo python -c “import sys; main=lambda:sys.stderr.write(‘Download failedn’); exec(sys.stdin.read()); main()”
How to install Python environment in my own account on Dreamhost?
Posted onHow to install Python environment in my own account on Dreamhost so that my application can use the python that I installed by myself instead of the system-wide one? The post for Bluehost works very well for Dreamhost too: https://my.bluehost.com/cgi/help/python-install In short: mkdir ~/python cd ~/python wget http://www.python.org/ftp/python/2.7.2/Python-2.7.2.tgz tar zxfv Python-2.7.2.tgz find ~/python -type d
Read more
How to automatically fill a batch of data to the PDF form
Posted onHow to automatically fill a batch of data (well structured) to the PDF form? There are lots data items, so automatically do this by a program/script is required. If you know a little bit of Python, you can achieve this in Python with some libraries. Check How can I auto-populate a PDF form in Django/Python.
Read more