Found 10784 Articles for Python

What are .pyc files in Python?

Rajendra Dharmkar
Updated on 27-Aug-2023 12:13:53

31K+ Views

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 31-May-2024 12:20:05

80K+ Views

The task or process of importing other files in Python allows you to use functions, classes, or variables defined in those files within your current Python script. In this article, we will explore different ways to import other Python files, including built-in modules, user-defined modules, and importing specific functions or variables. By the end of this article, you will have a solid understanding of how to import and use code from other Python files in your current projects. Along the way, we will use code examples followed by lucid explanations to help you understand the above task. Importing a User-Defined ... Read More

How to write binary data to a file using Python?

Rajendra Dharmkar
Updated on 25-Jul-2023 09:59:05

27K+ Views

It is a common and routine task in Python; the act of writing binary data to a file when you need to save non-textual data such as images, audio files, or serialized objects. In this article, we will explore various ways how you can write binary data to a file using Python and provide you with code examples followed by comprehensive explanations to help you grasp the concept. To write binary data to a file in Python, you can follow these steps: Open the File in Binary Mode First, you should proceed to open the file in ... Read More

How to set creation and modification date/time of a file using Python?

Rajendra Dharmkar
Updated on 25-Jul-2023 09:41:43

7K+ Views

The creation and modification datetime of a file in Python are defined as the timestamps associated with the events of when the file was created and when it was last modified. Creation datetime: It is defined as the timestamp when a file was initially created or added to the file system. Modification datetime: It is defined as the timestamp when the file's content was last modified or updated. These datetimes can provide a lot of information such as the file's age, recent changes, or when it was first introduced. In Python, you can fetch the ... Read More

How i can replace number with string using Python?

Malhar Lathkar
Updated on 20-Jun-2020 09:05:51

1K+ Views

For this purpose let us use a dictionary object having digit as key and its word representation as value −dct={'0':'zero', '1':'one', '2':'two', '3':'three', '4':'four',      '5':'five', '6':'six', '7':'seven', '8':'eight', '9':'nine'Initializa a new string object newstr=''Using a for loop traverse each character  ch from input string at check if it is a digit with the help of isdigit() function. If it is digit, use it as key and find corresponding value from dictionary and append it to newstr. If not append the character ch itself to newstr. Complete code is as follows:string='I have 3 Networking books, 0 Database books, and 8 Programming ... Read More

How to remove a file using Python?

Rajendra Dharmkar
Updated on 24-Jul-2023 20:42:18

254 Views

There will be times when we need to remove files programmatically in Python. Removing files, in this context, is the act of deleting or erasing files from a computer's file system using Python programming language. You must know that when you remove a file, it is permanently deleted from the storage location; this frees up disk space and makes the file inaccessible. Python has a number of modules and functions, such as os.remove() or os.unlink(), that permit you to interact with the operating system and remove files using code. This functionality is particularly useful when we want to automate tasks, ... Read More

How to create a unique directory name using Python?

Rajendra Dharmkar
Updated on 18-Feb-2020 04:56:12

815 Views

You can use the tempfile module to create a unique temporary directory in the most secure manner possible. There are no race conditions in the directory’s creation. The directory is readable, writable and searchable only by the creating user ID. Note that the user of mkdtemp() is responsible for deleting the temporary directory when done with it. To create a new temporary directory, use it as follows −import tempfile _, temp_dir_path = tempfile.mkdtemp() # Do what you want with this directory # And remove the directory when doneNote that you need to manually delete this directory after you're done with ... Read More

How to create a unique temporary file name using Python?

Rajendra Dharmkar
Updated on 18-Feb-2020 04:54:54

628 Views

You can use the tempfile module to create a unique temporary file in the most secure manner possible. There are no race conditions in the file’s creation. The file is readable and writable only by the creating user ID. Note that the user of mkstemp() is responsible for deleting the temporary file when done with it. To create a new temporary file, use it as follows −import tempfile _, temp_file_path = tempfile.mkstemp() print("File path: " + temp_file_path)Note that you need to manually delete this file after you're done with it.

How to rename multiple files recursively using Python?

Rajendra Dharmkar
Updated on 25-Jul-2023 09:36:59

3K+ Views

The act of renaming multiple files recursively in Python can be a useful task when it is required to change the names of multiple files within a directory and its subdirectories. If you need to replace certain characters, add prefixes or suffixes, or completely change the file names, Python has powerful tools to accomplish such operations. In this article, we explore a few different approaches to renaming multiple files recursively using Python. We consider some code examples, each demonstrating a unique method to achieve this task. So let us get started right away and learn how to rename multiple files ... Read More

How to rename directory using Python?

Rajendra Dharmkar
Updated on 25-Jul-2023 09:31:07

8K+ Views

There are times when you want to organize your files or update the directory names to better reflect their contents. In such scenarios, the operation of renaming directories becomes helpful and it is a common task that we often encounter in Python file management. There is provision of several modules and methods in Python that makes renaming directories an easy task. In this article, we explore different ways how you can rename directories using Python code. You will learn this skill by practicing the code examples and explanations discussed in this article. Making Use of the OS Module ... Read More

Advertisements