- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 9760 Articles for Python

Updated on 13-Dec-2019 11:00:39
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 simple program:import time from watchdog.observers import Observer from watchdog.events import FileSystemEventHandler class MyHandler(FileSystemEventHandler): def on_modified(self, event): print("Got it!") event_handler = MyHandler() observer = Observer() observer.schedule(event_handler, path='.', recursive=False) observer.start() try: while True: time.sleep(1) except KeyboardInterrupt: observer.stop() observer.join() When ... Read More 
Updated on 13-Dec-2019 10:52:18
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 you save this file as cat.py and run it using:$ python cat.py my_file.txtThen open my_file.txt, you'll see at the end, Hello is written. The above command will take the my_file.txt and pass it to cat.py as a CLI argument in variable argv[1](second command line argument) which we can use to ... Read More 
Updated on 19-May-2023 14:34:43
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 that, you could simply write a program:import sys with open(sys.argv[1], 'r') as f: contents = f.read() print contentsIf you save this file as cat.py and run it using:$ python cat.py my_file.txtThis will take the my_file.txt and pass it to cat.py as a CLI argument in variable argv[1](second command ... Read More 
Updated on 13-Dec-2019 10:49:03
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 function called my_func, you could import it:
from my_module import my_func
# Or you could import the module as a whole,
import my_module 
Updated on 13-Dec-2019 10:47:39
To call a Python file from within a PHP file, you need to call it using the shell_exec function.For exampleThis will call the script. But in your script at the top, you'll need to specify the interpreter as well. So in your py file, add the following line at the top:#!/usr/bin/env pythonAlternatively you can also provide the interpreter when executing the command.For example 
Updated on 13-Dec-2019 11:04:08
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 called my_song.mp3 and want to split it in files of size 500 bytes each.CHUNK_SIZE = 500 file_number = 1 with open('my_song.mp3') as f: chunk = f.read(CHUNK_SIZE) while chunk: with open('my_song_part_' + str(file_number)) as chunk_file: chunk_file.write(chunk) ... Read More 
Updated on 13-Dec-2019 11:02:32
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 you'd writeimport fileBNow in fileA, you can call any function inside fileB like:fileB.my_func()In file B you have to define a function called my_func before you can use it in any other file.Exampledef my_func(): print("Hello from B")OutputNow when you run fileA.py, you will get the output:Hello from BRead More 
Updated on 13-Dec-2019 11:06:43
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 specific values.The underscore is also used for ignoring the specific values in several languages like elixir, erlang, python, etc. If you don’t need the specific values or the values are not used, just assign the values to underscore.For example>>> _, _, a = (1, 2, 3) >>> a 33. To ... Read More 
Updated on 13-Dec-2019 10:14:47
There are multiple ways to make one Python file run another.1. 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 you'd writeimport fileBNow in fileA, you can call any function inside fileB like:fileB.my_func()2. You can use the exec command. execfile('file.py') executes the file.py file in the interpreter.3. You can spawn a new process using the os.system command.For exampleos.system('python my_file.py')Read More 
Updated on 13-Dec-2019 10:41:42
The shutil module provides functions for copying files, as well as entire folders.Calling shutil.copy(source, destination) will copy the file at the path source to the folder at the path destination. (Both source and destination are strings.) If destination is a filename, it will be used as the new name of the copied file. This function returns a string of the path of the copied file.For example>>> import shutil >>> # Copy the file in same folder with different name >>> shutil.copy('original.txt', 'duplicate.txt') '/home/username/duplicate.txt' >>> shutil.copy('original.txt', 'my_folder/duplicate.txt') '/home/username/my_folder/duplicate.txt'The same process can be used to copy binary files as well.Read More Advertisements