Found 9783 Articles for Python

How to print all the values of a dictionary in Python?

Sarika Singh
Updated on 18-Aug-2022 10:34:18
Python has a built-in method called values() that returns a view object. The dictionary's values are listed in the view object. We can use the values() method in Python to retrieve all values from a dictionary. Python's built-in values() method returns a view object that represents a list of dictionaries containing every value. This article explains about the various ways to print all the values of a dictionary in Python. Using dict.values() Method Python's dict.values() method can be used to retrieve the dictionary values, which can then be printed using the print() function. Example Following is an example to print ... Read More

How to print all the keys of a dictionary in Python

Sarika Singh
Updated on 18-Aug-2022 10:11:55
An unordered collection of data values is what a Python dictionary is. A Python dictionary contains a key: value pair, in contrast to other data structures that only include one value per entry. This article explains about the various ways to print all the keys of a dictionary in Python. Using dict.keys() Method Python's dict.keys() method can be used to retrieve the dictionary keys, which can then be printed using the print() function. A view object that shows a list of every key in the dictionary is the result of the dict.keys() method. The dictionary's elements can be accessed using ... Read More

How do we specify the buffer size when opening a file in Python?

Rajendra Dharmkar
Updated on 18-Feb-2020 06:50:13
If you look at the function definition of open - open(name[, mode[, buffering]]), you'll see that it takes 3 arguments in Python 2, third one being buffering. The optional buffering argument specifies the file’s desired buffer size: 0 means unbuffered, 1 means line buffered, any other positive value means use a buffer of (approximately) that size (in bytes). A negative buffering means to use the system default, which is usually line buffered for tty devices and fully buffered for other files. If omitted, the system default is used.For example, If you want to open a file with a buffer size ... Read More

What does the 'U' modifier do when a file is opened using Python?

Rajendra Dharmkar
Updated on 18-Feb-2020 06:20:55
In a Python with universal newline support open() the mode parameter can also be "U", meaning "open for input as a text file with universal newline interpretation". This is needed for crossplatform support as newlines on Unix os are represented by a single character while those on windows are represented by 2 characters \r. When opened in Python, All line ending conventions will be translated to a "" in the strings returned by the various file methods such as read() and readline(). For example you have a file on windows with the text −ExampleHello\rworld When you open it in ... Read More

What does the 'b' modifier do when a file is opened using Python?

Sarika Singh
Updated on 14-Nov-2022 08:24:56
If we open a file in oython using the b modifier. The file is opened in binary mode using the 'b' modifier. Any file whose format doesn't consist of readable characters is referred to as a "binary" file. Binary files include audio files like MP3s, text formats like Word or PDF, and image files like JPEGs or GIFs. Files are automatically opened in text mode in Python. When choosing a mode, include the letter "b" for binary mode. By default, the open() function opens a file in text format. As a result, the "wb" mode opens the file in binary ... Read More

What are the modes a file can be opened using Python?

Rajendra Dharmkar
Updated on 18-Feb-2020 06:19:10
Files in python can be opened in the following modes.ModeDescription'r'Read mode. (default)'w'Write mode. Creates a new file if it does not exist or truncates the file if it exists.'x'Open a file for exclusive creation. If the file already exists, the operation fails.'a'  Appending at the end of the file without truncating it. Creates a new file if it does not exist.'t'Open in text mode. (default)'b'Open in binary mode.'+'  Open a file for updating (reading and writing) These modes can be used in combinations and need to be passed as the second argument when opening a file. If you don't specify a ... Read More

How to check file last access time using Python?

Sarika Singh
Updated on 18-Aug-2022 08:29:35
The file last access datetime in Python can be obtained in a number of different ways. The following OS module methods will be used to obtain the file last access time in Python. Using os.path.getatime() Method In Python, we can use the os.path.getatime() method to retrieve a path's most recent access time. The path that we need to verify for the access time is taken by this approach. The amount of time since the epoch is returned as a floating point value. It throws one OSError if the requested path cannot be reached or if it does not exist. Syntax ... Read More

How to remove swap files using Python?

Sarika Singh
Updated on 17-Aug-2022 14:03:51
This Python article will teach you how to recursively remove all files in a folder that have a particular extension. The application will remove all files with the supplied extension inside the folder when we give it the folder path and file extension. Example - using file.endswith() method The steps involved to remove swap files are as follows − Import the _os _module and _listdir _from it. To view a list of all files in a certain folder, use _listdir, and to delete a file, use _os module. The path to the folder containing all files is called folderpath. ... Read More

How to remove hidden files and folders using Python?

Rajendra Dharmkar
Updated on 16-Dec-2019 07:11:30
On Unix OS(OSX, Linux, etc) hidden files start with a '.' so we can filter them out using a simple startswith check. On windows, we need to check file attributes and then determine whether the file/folder is hidden or not.ExampleFor example, you can use the following code to delete all hidden files:import os if os.name == 'nt':     import win32api, win32con def file_is_hidden(p):     if os.name== 'nt':         attribute = win32api.GetFileAttributes(p)         return attribute & (win32con.FILE_ATTRIBUTE_HIDDEN | win32con.FILE_ATTRIBUTE_SYSTEM)     else:         return p.startswith('.') #linux-osx [os.remove(f) for f in ... Read More

How to list non-hidden files and directories in windows using Python?

Rajendra Dharmkar
Updated on 16-Dec-2019 07:22:24
On Unix OS(OSX, Linux, etc) hidden files start with a '.' so we can filter them out using a simple startswith check. On windows, we need to check file attributes and then determine whether the file is hidden or not.ExampleFor example, you can use the following code to get a listing without hidden files:import os if os.name == 'nt':     import win32api, win32con def file_is_hidden(p):     if os.name== 'nt':         attribute = win32api.GetFileAttributes(p)         return attribute & (win32con.FILE_ATTRIBUTE_HIDDEN | win32con.FILE_ATTRIBUTE_SYSTEM)     else:         return p.startswith('.') #linux-osx file_list = ... Read More
Advertisements