How to find current directory of program execution in Python?

Rajendra Dharmkar
Published on 26-Dec-2017 13:32:17
To know the current working directory or pwd use the os module.For example>>> import os >>> print(os.getcwd()) /home/ayush/qna

How to setup VIM autoindentation properly for editing Python files?

Rajendra Dharmkar
Published on 26-Dec-2017 13:30:07
To set autoindent for python files in VIM, use the following config:Exampleset ts=4 set sts=4 set sw=4 set autoindent set smartindent set smarttab set expandtabAdd this config to your .vimrc file in home folder.

How to monitor Python files for changes?

Rajendra Dharmkar
Published on 26-Dec-2017 13:27:28
Monitoring files for changes in any language is hard because of cross platform issues. On python, there is a widely used cross platform library called watchdog that allows watching for changes. You can install it using:$ pip install watchdogExampleTo watch a file called 'my_file.txt' using watchdog, you can use the ... Read More

How to write into a file from command line using Python?

Rajendra Dharmkar
Published on 26-Dec-2017 13:21:07
In order to write to a file from command line using Python, the script you want to use for it needs to accept a CLI argument.ExampleFor example, you want to write a program that appends "Hello" to any file it opens:import sys with open(sys.argv[1], 'a') as f:     f.write("Hello")OutputIf ... Read More

How to read a file from command line using Python?

Rajendra Dharmkar
Published on 26-Dec-2017 12:47:48
In order to read a file form command line using Python, the script you want to use for it needs to accept a CLI argument. For example, say you want to write a cat command in python(a command that dumps all file content on the terminal). In order to do ... Read More

How to import a Python module given the full path?

Rajendra Dharmkar
Published on 26-Dec-2017 12:45:27
The easiest way to import a Python module, given the full path is to add the path to the path variable. The path variable contains the directories Python interpreter looks in for finding modules that were imported in the source files.For exampleimport sys sys.path.append('/foo/bar/my_module') # Considering your module contains a ... Read More

How to call Python file from within PHP?

Rajendra Dharmkar
Published on 26-Dec-2017 12:42:15
To call a Python file from within a PHP file, you need to call it using the shell_exec function.For example<?php     $command = escapeshellcmd('/usr/custom/test.py');     $output = shell_exec($command);     echo $output; ?>This will call the script. But in your script at the top, you'll need to specify ... Read More

How to spilt a binary file into multiple files using Python?

Rajendra Dharmkar
Published on 26-Dec-2017 12:39:00
To split a big binary file in multiple files, you should first read the file by the size of chunk you want to create, then write that chunk to a file, read the next chunk and repeat until you reach the end of original file.ExampleFor example, you have a file ... Read More

How can I source a Python file from another Python file?

Rajendra Dharmkar
Published on 26-Dec-2017 12:21:00
In order to source a Python file from another python file, you have to use it like a module. import the file you want to run and run its functions. For example, say you want to import fileB.py into fileA.py, assuming the files are in the same directory, inside fileA ... Read More

How does underscore "_" work in Python files?

Rajendra Dharmkar
Published on 26-Dec-2017 12:15:48
The underscore (_) is special in Python. There are 5 cases for using the underscore in Python.1. For storing the value of last expression in interpreter.The python interpreter stores the last expression value to the special variable called ‘_’.For example>>> 12 + 10 22 >>> _ 222. For ignoring the ... Read More
Advertisements