Found 9783 Articles for Python

How does underscore "_" work in Python files?

Rajendra Dharmkar
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

How can I make one Python file run another?

Rajendra Dharmkar
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 into, 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('') executes the file in the interpreter.3. You can spawn a new process using the os.system command.For exampleos.system('python')Read More

How do I copy a binary file in Python?

Rajendra Dharmkar
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

How to find all files in a directory with extension .txt in Python?

Rajendra Dharmkar
Updated on 13-Dec-2019 10:40:41
You can use the os.listdir method to get all directories and files in a directory. Then filter the list to get only the files and check their extensions as well.For example>>> import os >>> file_list = [f for f in os.listdir('.') if os.path.isfile(os.path.join('.', f)) and f.endswith('.txt')] >>> print file_list ['LICENSE.txt', 'NEWS.txt', 'README.txt']The endswith method is a member of string class that checks if a string ends with a certain suffix.You can also use the glob module to achieve the same:>>> import glob, os >>> file_list = [f for f in glob.glob("*.txt")] >>> print file_list ['LICENSE.txt', 'NEWS.txt', 'README.txt']Read More

How do I list all files of a directory in Python?

Rajendra Dharmkar
Updated on 13-Dec-2019 10:38:43
os.listdir(my_path) will get you everything that's in the my_path directory - files and directories.ExampleYou can use it as follows:>>> import os >>> os.listdir('.') ['DLLs', 'Doc', 'etc', 'include', 'Lib', 'libs', 'LICENSE.txt', 'NEWS.txt', 'python.exe', 'pythonw.exe', 'README.txt', 'Scripts', 'share', 'tcl', 'Tools', 'w9xpopen.exe']OutputIf you want just files, you can filter it using isfile:>>> import os >>> file_list = [f for f in os.listdir('.') if os.path.isfile(os.path.join('.', f))] >>> print file_list ['LICENSE.txt', 'NEWS.txt', 'python.exe', 'pythonw.exe', 'README.txt', 'w9xpopen.exe']Read More

What is the common header format of Python files?

Rajendra Dharmkar
Updated on 18-Feb-2020 05:03:20
This is how Python developers commonly organize their modules/python file −The first line of each file shoud be #!/usr/bin/env python. This makes it possible to run the file as a script invoking the interpreter implicitly.Next should be the docstring with a description.All code, including import statements, should follow the docstring. Import built-in modules first, followed by third-party modules, followed by any changes to the path and your own modules.

What do the python file extensions, .pyc .pyd .pyo stand for?

Rajendra Dharmkar
Updated on 02-May-2023 13:52:24
The .py, .pyc, .pyo and .pyd files have their own significance when it comes to executing python programs. They are used for − .py: The input source code that you've written. .pyc: The compiled bytecode. If you import a module, python will build a *.pyc file that contains the bytecode to make importing it again later easier (and faster). .pyo: A *.pyc file that was created while optimizations (-O) was on. .pyd: A windows dll file for Python. In Python, there are several file extensions that are used to indicate different types of files. Here are some of the most ... Read More

What are .pyc files in Python?

Rajendra Dharmkar
Updated on 27-Mar-2023 17:19:04
In Python, .pyc files are compiled bytecode files that are generated by the Python interpreter when a Python script is imported or executed. The .pyc files contain compiled bytecode that can be executed directly by the interpreter, without the need to recompile the source code every time the script is run. This can result in faster script execution times, especially for large scripts or modules. .pyc files are created by the Python interpreter when a .py file is imported. They contain the "compiled bytecode" of the imported module/program so that the "translation" from source code to bytecode (which only needs ... Read More

How to import other Python files?

Rajendra Dharmkar
Updated on 13-Dec-2019 10:37:17
To use any package in your code, you must first make it accessible. You have to import it. You can't use anything in Python before it is defined. Some things are built in, for example the basic types (like int, float, etc) can be used whenever you want. But most things you will want to do will need a little more than that. For example, if you want to calculate cosine of 1 radian, if you run math.cos(0), you'll get a NameError as math is not defined.ExampleYou need to tell python to first import that module in your code so ... Read More

How to write binary data to a file using Python?

Rajendra Dharmkar
Updated on 13-Dec-2019 10:31:33
"Binary" files are any files where the format isn't made up of readable characters. Binary files can range from image files like JPEGs or GIFs, audio files like MP3s or binary document formats like Word or PDF. In Python, files are opened in text mode by default. To open files in binary mode, when specifying a mode, add 'b' to it.For examplef = open('my_file', 'w+b') byte_arr = [120, 3, 255, 0, 100] binary_format = bytearray(byte_arr) f.write(binary_format) f.close()This opens a file in binary write mode and writes the byte_arr array contents as bytes in the binary file, my_file.Read More