Python File – Java2Blog https://java2blog.com A blog on Java, Python and C++ programming languages Tue, 05 Dec 2023 10:32:18 +0000 en-US hourly 1 https://wordpress.org/?v=6.2.9 https://java2blog.com/wp-content/webpc-passthru.php?src=https://java2blog.com/wp-content/uploads/2022/09/cropped-ICON_LOGO_TRANSPARENT-32x32.png&nocache=1 Python File – Java2Blog https://java2blog.com 32 32 Read File without Newline in Python https://java2blog.com/read-file-without-newline-python/?utm_source=rss&utm_medium=rss&utm_campaign=read-file-without-newline-python https://java2blog.com/read-file-without-newline-python/#respond Wed, 08 Mar 2023 09:43:55 +0000 https://java2blog.com/?p=23012 Read File without NewLine in Python

In Python, there are multiple ways to read a file without new lines, but before diving into those methods, let’s look at the .txt file we will use for this article.

This is line one.
This is line two.
This is line three.
And this is line four.

You can follow along using the file.txt or your own file.

Use read() and replace() Methods

To read a file without newline in Python:

  • Use the open() method to open the text file.
  • Use the read() with the replace() method to replace newline characters with an empty string
with open('file.txt') as file:
    content_without_newline = file.read().replace('\n', '')
print(content_without_newline)
This is line one.This is line two.This is line three.And this is line four.

First, we used the open() method, which took the file.txt as an argument, opened it in read mode and returned its file instance (also called file object) that we saved in the file variable.

Then, we used this file object to access the read() method, which read the entire file.txt as a string with which we chained the replace() method to replace the new line characters (represented with \n) with an empty string (represented with ''). Finally, we saved the updated content in the content_without_newline variable that we passed to the print() method to display on the screen.

This solution is similar to remove newline from String in Python.

The open() method can take other modes that we can see here.

Use readlines(), join(), & repalce() Methods

To read a file without newline in Python:

  • Use the open() method to open the specified text file in read mode.
  • Use readlines() to read the text file line by line.
  • Use the join() method to join all the lines that we read using the readlines() method
  • Use the replace() method to replace newline characters with an empty string.
with open('file.txt') as file:
    content_without_newlines = ''.join(file.readlines()).replace('\n', '')
print(content_without_newlines)
This is line one.This is line two.This is line three.And this is line four.

Again, we used the open() method to open the file.txt in read mode and saved its file object in the file variable. Next, we used the .readlines() method to read the file.txt line by line, where each line will be a separate string; every string was passed to the join() method to join without any separator because we used an empty string (denoted with '') as a delimiter here.

At this point, we have a single string but with newline characters. Now, it’s time to use the replace() method to do replacements. This method took two arguments; first was the \n representing a new line character while the second was '' denoted empty string; in the above code snippet, we replaced \n with '', saved the updated content in content_without_newlines variable. Finally, we passed the content_without_newlines to the print() method to print on the console.

Use read(), join(), and splitlines() Methods

To read a file without newline in Python:

  • Use the open() method to open the text file in read mode.
  • Use the read() method to read the entire text file as a string.
  • Use splitlines() method to split the string (produced by read() method) into list of strings.
  • Use join() method to join all the lines produced by splitlines() method.
with open('file.txt') as file:
    content_without_newlines = ''.join(file.read().splitlines())
print(content_without_newlines)
This is line one.This is line two.This is line three.And this is line four.

We have already learned about open(), read(), and join() methods in previous examples. Here, we used the splitlines(), which split the lines but why and how? For this, let’s understand the code in sequential steps. First, the open() method opened the file.txt in read mode, the read() method read the entire file as one string, and the splitlines() method split that string into a list of strings where each list item corresponds to one line in file.txt.

The splitlines() automatically split the string, produced by the read() method, based on the \n because every line ended with \n in file.txt. Remember that the splitlines() method automatically handles the line ending conventions such as \r\n, \r or \n. Finally, we saved the updated string in a variable that we passed to the print() method to print the updated content on the screen.

Use for Loop with strip() Method

To read a file without newline in Python:

  • Use the open() method to open the text file in read mode.
  • Use the for loop to read text file line by line.
  • In each iteration:
    • Use the strip() method to remove all leading and trailing whitespaces from the current line.
    • Use the += operator to concatenate the current updated line.
with open('file.txt') as f:
    content_without_newlines = ''
    for current_line in file:
        content_without_newlines += current_line.strip()
print(content_without_newlines)
This is line one.This is line two.This is line three.And this is line four.

Here, we used the open() method to open the file.txt in read mode and stored its file instance in the file variable. Then, we declared and initialized the content_without_newlines variable with an empty string. This variable will be used to contain the content without newline characters.

Next, we used the for loop to iterate over all lines of the specified text file. Then, we used the strip() method for every line to remove leading and trailing whitespaces and concatenated this line with the content_without_newlines value. Finally, we printed the value of the content_without_newlines variable on the console.

]]>
https://java2blog.com/read-file-without-newline-python/feed/ 0
Count Files in Directory in Python https://java2blog.com/python-count-files-directory/?utm_source=rss&utm_medium=rss&utm_campaign=python-count-files-directory https://java2blog.com/python-count-files-directory/#respond Mon, 13 Feb 2023 11:03:59 +0000 https://java2blog.com/?p=22483 Using os Module

We can use the listdir(), scandir(), and walk() methods of the os module to count files in the current directory, including/excluding sub-directories. Before diving into the details, let’s look at the directory structure we will use for this article; you may use your own.

  • E:\Test – contains two text files (File1.txt and File2.txt) and two folders (FolderA and FolderB).

  • E:\Test\FolderA – contains four text files (File1.txt, File2.txt, File3.txt, and File4.txt).

  • E:\Test\FolderB – contains two text files (File1.txt and File2.txt).

Use os.listdir() Method

To count files in the current directory, excluding sub-directories:

  • Use a for loop to iterate over the current directory.
  • Use the if statement with the isfile() method to check whether the current item is a file.
    If it is, then increment the file counter by 1.
import os

directory_path = "E:\Test"
count = 0

for item in os.listdir(directory_path):
        item_path = os.path.join(directory_path, item)
        if os.path.isfile(item_path):
            count += 1

print('File Count:', count)
File Count: 2

After importing the os module, we declared and initialized two variables; the directory_path with the current directory, which is E:\Test in our case, and the count with 0. The count variable was used to maintain the number of files in the current top directory.

Next, we utilized the for loop to iterate over all items (directories and files) in the specified directory using the os.listdir() method. This method returned a list of all items residing in the current directory, which we specified using directory_path. Then, this loop iterated over every list item and assigned that item to the item variable.

For each item, we used the os.path.join() method, which took the directory_path and item as the arguments to create a complete path by joining the directory_path with the item name. Then, we saved this full path in the item_path variable, which was passed to the os.path.isfile() method in the if statement to check whether it is a file.

If the item is a file, increment the count variable by 1. The above code does not check whether the item is a directory or not, and it does not recursively go through subdirectories. So it will only count the files in the specified directory and not in its subdirectories.

Similarly, we can use the following code to count files in the current directory, including sub-directories:

import os

dir_path = "E:\Test"
count = 0

for item in os.listdir(dir_path):
        item_path = os.path.join(dir_path, item)

        if os.path.isfile(item_path):
            count += 1

        elif os.path.isdir(item_path):
            for sub_item in os.listdir(item_path):
                sub_item_path = os.path.join(item_path, sub_item)
                if os.path.isfile(sub_item_path):
                    count += 1

print('File Count:', count)
File Count: 8

The above code block is similar to the previous example. Here, we added an elif statement to check whether the current item is a directory or not using the isdir() method. If it is a directory, we use a nested for loop to iterate over the sub-directory and count files. You might have noticed the redundancy in the above; we can define a function to make it more organized and clear; see the following code:

import os

count = 0

def count_files(path):
    global count

    for item in os.listdir(path):
        item_path = os.path.join(path, item)

        if os.path.isfile(item_path):
            count += 1
        elif os.path.isdir(item_path):
            count_files(item_path)

count_files("E:\Test")
print('File Count:', count)
File Count: 8

NOTE: We used recursion to count files in the current directory and sub-directories because the count_files() function calls a copy of itself.

Use os.scandir() Method

To count files in the current directory, excluding sub-directories:

  • Use the os.scandir() method to loop over the current directory.
  • Use the if statement with the isfile() method to check whether it is a file. If it is, then increment the counter by 1.
import os

count = 0
dir_path = "E:\Test"

for path in os.scandir(dir_path):
    if path.is_file():
        count += 1

print('File Count:', count)
File Count: 2

Here, we used a for loop with the os.scandir() method to iterate over all the items (files and directories) in the specified directory, which is dir_path. The os.scandir() method returned an iterator that yields DirEntry objects which contain information about the directory entry.

The iterator is efficient because it doesn’t need to generate a list of all the items in the directory before iterating, which can be faster than using os.listdir(), mainly when the directory contains a large number of files.

For each iteration, the variable path contained an instance of os.DirEntry representing a file or directory in the specified directory. We then used the is_file() function on the DirEntry object to check whether the entry is a file.

This code does not recursively go through sub-directories. To perform actions on the sub-directories, we used recursion as follows to count files in the current directory, including sub-directories:

import os

count = 0

def count_files(directory_path):
    global count

    for path in os.scandir(directory_path):
        if path.is_file():
            count += 1
        else:
            count_files(path)

count_files("E:\Test")
print('File Count:', count)
File Count: 8

This code is similar to the previous one except for two differences. First, we used a global variable named count to use it anywhere in the program. Second, we added an else block to call the copy of the count_files() function to loop over sub-directories and count files from there.

NOTE: The os.scandir() is available in Python 3.5 and above.

Use os.walk() Method

To count files in the current directory, excluding sub-directories:

  • Use a for loop with the os.walk() method to iterate over the current directory.
  • Use a nested for loop to iterate over files in the current directory.
  • Use the if statement to check if the current root directory is the same as the given directory; if it is, then increment the file counter by 1.
  • Use the break statement to break the nested for loop after the first iteration.
import os

current_dir = "E:\Test"
file_count = 0

for root, dirs, files in os.walk(current_dir):
    for file in files:
        if root == current_dir:
            file_count += 1
    break

print('File Count', file_count)
File Count: 2

This code used the os.walk() function to iterate over all the files in the directory specified by current_dir and its subdirectories. The os.walk() function generated the file names in the directory tree by walking a tree either top-down or bottom-up. Each directory in a tree rooted at the top (including the top itself) yields a 3-tuple (dirpath, dirnames, filenames).

The root variable contained the current directory being processed, dirs was a list of subdirectories in the current directory and files was a list of files in the current directory. We then iterated over the files list to check if the current root directory is equal to current_dir.

If it is, it incremented the file_count variable by 1. We used the break statement at the end of the nested for loop caused the loop to exit after the first iteration, so only the files in the top-level directory (i.e., current_dir) are counted.

The above code only counted the files in the top-level directory, not in its subdirectories. Use the following code to count files in the current directory, including sub-directories:

import os

current_dir = "E:\Test"
file_count = 0

for root, dirs, files in os.walk(current_dir):
    file_count += len(files)

print('File Count:', file_count)
File Count: 8

This code also used the os.walk() method we have already learned. We used the len() method, taking the files list as a parameter to count files in the current top directory and sub-directories. The len() method recursively went through all the subdirectories and counted all the files.

Note that root and dirs are not used in the above code and are not necessary here to count files, but we have to define them in the for loop to correctly save three tuples (dirpath, dirnames, filenames) returned by the os.walk() method.

Using pathlib Module

To count files in the current directory, excluding sub-directories:

  • Use a for loop to iterate over the specified directory.
  • Use the if statement with the is_file() method to check if the current item is a file; if it is, increment the file counter by 1.
import pathlib

file_count = 0

for path in pathlib.Path("E:\Test").iterdir():
    if path.is_file():
        file_count += 1

print('File Count:', file_count)
File Count: 2

Here, we used the pathlib module to count files in the current top directory. Here, we used the pathlib.Path().iterdir() method to iterate over all the items (files and directories) in the specified directory, which is E:\Test.

The pathlib module is a powerful object-oriented way of working with file and directory paths. For example, the pathlib.Path("E:\Test") creates a Path object of the directory E:\Test.

The iterdir() method was used to get an iterator over the files and directories in the directory. This method returned an iterator of Path objects, each representing a file or directory in the directory.

For each iteration, the variable path will contain a Path object representing a file or directory in the specified directory. We then used the is_file() method on the Path object to check whether the entry is a file.

The above code did not recursively go through subdirectories. To perform actions on the items, we’ll need to specify what we want to do with the items inside the for loop. Let’s learn while counting files in the current directory, including sub-directories.

from pathlib import Path

current_dir = Path("E:\Test")
file_count = 0

for path in current_dir.rglob('*'):
    if path.is_file():
        file_count += 1

print('File Count: ', file_count)
File Count: 8

We used the pathlib.Path() constructor for the above code snippet to create a Path object representing the directory E:\Test. The Path object can interact with the file system to perform various operations such as reading, writing, and manipulating files and directories. This Path object was saved in the current_dir variable.

Next, we defined a file_count variable to maintain the file count; for now, we initialized it with 0. Finally, we used the current_dir.rglob() method to recursively iterate over all the files in the directory specified by the current_dir path object.

The rglob(*) method returned an iterator that yields all the files matching the specified pattern. We used the * (a wildcard character), which matched any number of characters, so it returned all the files in the directory and all its subdirectories.

For each iteration, the variable path contained a Path object representing a file or directory. Then we used the if statement with the path.is_file() method to check if the current item is a file. If it is a file, we increment the file_count variable by 1.

NOTE: The rglob() method is available in Python 3.5 and above, while the pathlib is available in Python 3.4 and above.

That’s all about how to count files in Directory in Python.

]]>
https://java2blog.com/python-count-files-directory/feed/ 0
Save Object to File in Python https://java2blog.com/save-object-to-file-python/?utm_source=rss&utm_medium=rss&utm_campaign=save-object-to-file-python https://java2blog.com/save-object-to-file-python/#respond Wed, 21 Dec 2022 06:17:16 +0000 https://java2blog.com/?p=21929 1. Introduction

In Python, one often encounters the need to save complex objects, like instances of a class, to a file for purposes such as data persistence. Let’s take a Student class as an example, which includes attributes like name, email, age, city, courses, and address. The goal is to efficiently serialize this object into a file and then deserialize it back into a class instance while maintaining the integrity of the data.

2. Using pickle.dump() Function

To save object to file in Python:

  • Use the open() function with the with keyword to open the specified file.
  • Use the pickle.dump() function to convert the object to a binary format to write it to the file.
import pickle
class Student(object):
    def __init__(self, name, email, age, city, courses, address):
        self.name = name
        self.email = email
        self.age = age
        self.city = city
        self.courses = courses
        self.address = address

std1 = Student(
    name='Anonymous',
    email='@gmail.com',
    age=36,
    city='London',
    courses=['Web', 'OOP'],
    address={
        'streetAddress': '100A',
        'town': 'My Town'
    }
)
file_name = 'std1.pkl'
# Writing the student object to a file using pickle
with open(file_name, 'wb') as file:
    pickle.dump(std1, file)
    print(f'Object successfully saved to "{file_name}"')

# Reading the student object back from the file
with open("std1.pkl", "rb") as file:
    loaded_student = pickle.load(file)

print(f"Deserialized Student Object: {loaded_student.name}, {loaded_student.email}")
Object successfully saved to "std1.pkl"
Python’s pickle is a built-in module for serializing and de-serializing objects (data structures). It transforms an object into a sequence of bytes stored in memory or disk for later use in the same state.

We can save complete data structures such as lists, dictionaries, and custom classes. So, first, we imported the pickle module. Then we created a class Student() and an object std1 from it.

The with statement in Python is a control structure that simplifies code by handling exceptions and reducing the amount of code. It creates a context manager object responsible for managing the context block.

  • Entering this context block requires opening a file or allocating memory.
  • While exiting requires other activities like closing a file or deallocating memory.

We used the with statement to open the file using the open(file_name, 'wb') function that takes the filename and mode as arguments.

The pickle module provides the dump() function that converts a Python data structure into a byte stream format to write it to a binary file on the disk. For example, we used this method to convert std1 to binary stream format and wrote it to the file std1.pkl.

3. Using dill.dump() Function

Suppose we already have a Python library called dill installed. If we don’t have it, we can install it with pip install dill. To save an object to a file using the dill module:

  • Use the open() function wrapped in the with statement to open the file.
  • Use the dump.dill() function to save the object to the file.
import dill

class Student(object):
    def __init__(self, name, email, age, city, courses, address):
        self.name = name
        self.email = email
        self.age = age
        self.city = city
        self.courses = courses
        self.address = address

std1 = Student(
    name='Anonymous',
    email='@gmail.com',
    age=36,
    city='London',
    courses=['Web', 'OOP'],
    address={
        'streetAddress': '100A',
        'town': 'My Town'
    }
)
file_name = 'std1.pkl'
with open(file_name, 'wb') as file:
    dill.dump(std1, file)
    print(f'Object successfully saved to "{file_name}"')

# Reading the student object back from the file
with open("std1.pkl", "rb") as file:
    loaded_student = pickle.load(file)

print(f"Deserialized Student Object: {loaded_student.name}, {loaded_student.email}")
Code Explanation:
Object successfully saved to "std1.pkl"
Dill is an extension of Python’s pickle module, which we discussed while explaining the code snippet for using the pickle.dump() function. It stores complex data structures in an efficient format so different systems can use them in distributed applications such as web services.

It can also store multiple objects in a single file using its multi-pickling mode. We imported the module. Then, after successfully creating the object std1 from the class Student(), we used the with statement to open the file std1.pkl using the open() function.

The dump() method converts the object into binary format and saves them locally. It can create backups of important data or send data between two programs. For example, we used the dump() method to store the object std1 in the file std1.pkl.

4. Using pandas.to_pickle() Function

To save an object to a file using the pandas.to_pickle() function:

  • Use the with context manager with the open() function to open the file.
  • Use the pandas.to_pickle() function to store the object in a binary file.
import pandas as pd

class Student(object):
    def __init__(self, name, email, age, city, courses, address):
        self.name = name
        self.email = email
        self.age = age
        self.city = city
        self.courses = courses
        self.address = address

std1 = Student(
    name='Anonymous',
    email='@gmail.com',
    age=36,
    city='London',
    courses=['Web', 'OOP'],
    address={
        'streetAddress': '100A',
        'town': 'My Town'
    }
)
file_name = 'std1.pkl'
with open(file_name, 'wb') as file:
    pd.to_pickle(std1, file)
    print(f'Object successfully saved to "{file_name}"')

# Reading the student object back from the file
with open("std1.pkl", "rb") as file:
    loaded_student = pickle.load(file)

print(f"Deserialized Student Object: {loaded_student.name}, {loaded_student.email}")
Code Explanation:
Object successfully saved to "std1.pkl"
Python’s pandas is an open-source module that provides high-performance data structures and analysis tools.

The primary purpose of this library is to provide efficient manipulation, filtering, reshaping, and merging operations on numerical tables or data frames. It aims to be the fundamental high-level building block for Python’s practical, real-world data analysis.

Once we created the object std1 from the class Student(), we opened the file std1.pkl using the built-in open() function wrapped in the with statement, which we discussed in the code section using the pickle.dump() function.

The pandas library provides a to_pickle() function that converts an object into a series of bytes and saves it in a file for future reference and use, enabling users to transfer data between applications.

It uses the pickle module internally, which simplifies the process by abstracting away the code necessary for properly serializing an object before writing it out to disk. For example, we used this method to write the std1 object to the file std1.pkl.

5. Using JSON Module

JSON is a widely-used format for data interchange, but it requires custom handling for class instances.

Example:

import json

class StudentJSONEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, Student):
            return obj.__dict__
        return json.JSONEncoder.default(self, obj)

# Function to decode JSON back into a Student object
def decode_student(dct):
    return Student(**dct)

# Writing the student object to a JSON file
with open("student.json", "w") as file:
    json.dump(student, file, cls=StudentJSONEncoder)

# Reading the student object back from the JSON file
with open("student.json", "r") as file:
    loaded_student = json.load(file, object_hook=decode_student)

print(f"Deserialized Student Object: {loaded_student.name}, {loaded_student.email}")

Explanation:

  • A custom StudentJSONEncoder is defined to convert Student objects into a serializable format.
  • The decode_student function is used to reconstruct the Student object from the JSON data.
  • json.dump() and json.load() are used with these custom handlers for serialization and deserialization.

6. Using YAML Module

YAML is a user-friendly serialization format but requires custom handling for class instances.

Example:

import yaml

# Custom representation for Student objects in YAML
def student_representer(dumper, data):
    return dumper.represent_mapping('!Student', data.__dict__)

yaml.add_representer(Student, student_representer)

# Writing the student object to a YAML file
with open("student.yaml", "w") as file:
    yaml.dump(student, file)

# Reading the student object back from the YAML file
with open("student.yaml", "r") as file:
    loaded_student = yaml.load(file, Loader=yaml.Loader)

print(f"Deserialized Student Object: {loaded_student.name}, {loaded_student.email}")

Explanation:

  • A custom representer for the Student class is defined for YAML serialization.
  • yaml.dump() and yaml.load() are used for writing to and reading from the YAML file.

7. Comparing Performance

Comparing the performance of different serialization libraries used in Python, such as Pickle, JSON, Dill, Pandas’ to_pickle(), and YAML, involves evaluating several factors: speed, file size, and ease of use. Each library has its strengths and weaknesses, which I’ll outline below:

7.1. Pickle

  • Speed: Pickle is generally very fast, especially for Python-specific data structures and objects.
  • File Size: Creates relatively small files due to binary serialization, but the size can grow with complex objects.
  • Ease of Use: Very straightforward for Python users, no additional setup required.
  • Use Case Suitability: Best for Python-specific applications where data doesn’t need to be shared with other languages.

7.2. JSON

  • Speed: JSON serialization can be slower than Pickle, particularly for complex or deeply nested objects.
  • File Size: JSON files are larger compared to binary formats like Pickle, as they are text-based. However, they are easily compressible.
  • Ease of Use: Straightforward, especially with the standard library. Custom serialization and deserialization can add complexity.
  • Use Case Suitability: Ideal for web applications, data interchange between different languages, or when human readability is important.

7.3. Dill

  • Speed: Similar to Pickle in terms of speed, but can vary based on the complexity of the objects being serialized.
  • File Size: Comparable to Pickle, with file sizes increasing for more complex objects.
  • Ease of Use: As easy as Pickle but with extended capabilities to serialize more complex Python objects.
  • Use Case Suitability: Suitable for scenarios where Pickle falls short in terms of object complexity.

7.4. Pandas’ to_pickle()

  • Speed: Offers good performance, especially for DataFrame objects. Might be slower for highly complex data structures.
  • File Size: File size is generally larger than Pickle due to the nature of DataFrame storage.
  • Ease of Use: Very convenient for users already working within the Pandas ecosystem.
  • Use Case Suitability: Best for serializing DataFrame objects, especially in data analysis and scientific computing contexts.

7.5. YAML

  • Speed: YAML serialization and deserialization are slower compared to binary formats like Pickle and Dill.
  • File Size: YAML files are larger due to their text-based, human-readable format.
  • Ease of Use: YAML is easy to read and write, but serializing and deserializing custom objects requires additional setup.
  • Use Case Suitability: Ideal for configuration files, applications requiring human readability, and complex data structures.

8. Conclusion

In summary, the choice of serialization library in Python depends on the specific requirements of your application. If speed and file size are critical, and the data is Python-specific, Pickle or Dill are excellent choices. For interoperability and human readability, JSON and YAML are better suited, though they come with a performance trade-off. Pandas’ to_pickle() is particularly useful when working with DataFrame objects within the Pandas ecosystem.

]]> https://java2blog.com/save-object-to-file-python/feed/ 0 Write Binary File in Python https://java2blog.com/write-binary-file-python/?utm_source=rss&utm_medium=rss&utm_campaign=write-binary-file-python https://java2blog.com/write-binary-file-python/#respond Sat, 17 Dec 2022 19:03:03 +0000 https://java2blog.com/?p=21901 Use bytearray() Function

To write a binary file in Python:

  • Use the bytearray() function to convert the list of bytes to a bytearray type object.
  • Use a with clause with open() the method in write binary mode(wb)
  • Use write() method to write the byte_array to the file.
byte_list = [100, 56, 35, 94]
byte_array = bytearray(byte_list)

try:
    with open("bytes_array.txt", 'wb') as f:
        f.write(byte_array)
        print(str(byte_array) + " successfully stored in a file...")
except Exception as e:
    print(e)
bytearray(b'd8#^') successfully stored in a file...

We created a byte_list using a sequence of values from 0 to 255 in binary form.

The bytearray() function is a built-in function in Python that takes an iterable to convert it into an array of bytes.It is an object constructor for a mutable array of bytes, like a list, but unordered. From an iterable of integers, it only accepts values in the range(256), or the function throws a ValueError: byte must be in range(0, 256).

We used the bytearray() function to convert the byte_list to the byte_array. To successfully write the byte_array to the file, we used Python’s try-except control flow statement. It handles errors and exceptions without disrupting the flow of the program.

We used the with keyword to wrap the code block of writing the byte_array to the bytes_array.txt file. Next, we used the open() function to open a file for reading, or writing is a built-in function in Python. It took two arguments: the filename and the mode.

The mode is an optional argument. It defaults to r (read) if it’s not specified. We specified the mode as wb to create or truncate the existing file named bytes_array.txt as binary. The write() function wrote the byte_array to the bytes_array.txt file.

Use bytes() Function

To write a binary file in Python:

  • Use the bytes() function to convert the list of bytes to a bytes type object.
  • Use a with clause with open() the method in write binary mode(wb)
  • Use write() method to write the byte_array to the file.
byte_list = [100, 56, 35, 94]
byte_array = bytes(byte_list)

try:
    with open("bytes_array.txt", 'wb') as f:
        f.write(byte_array)
        print(str(byte_array) + " successfully stored in a file...")
except Exception as e:
    print(e)
b'd8#^' successfully stored in a file...

We have already discussed the creation of the byte_list, try-except clause, with keyword, and open() and write() functions while explaining the code snippet using the bytearray() function.

In this section, we used the bytes() function, similar to the bytearray() function but returned an immutable sequence of bytes type. We used this function to convert a byte_list to a byte_array. Once we got the byte_array, we used the write() function to write the byte_array to the bytes_array.txt file.

Use struct.pack() Function

To write a binary file in Python:

  • Use the struct.pack() function to convert the list of bytes to a bytes type object.
  • Use a with clause with open() the method in write binary mode(wb)
  • Use write() method to write the byte_array to the file.
import struct
byte_list = [100, 56, 35, 94]
byte_array = struct.pack('4B', *byte_list)
try:
    with open("bytes_array.txt", 'wb') as f:
        f.write(byte_array)
        print(str(byte_array) + " successfully stored in a file...")
except Exception as e:
    print(e)
b'd8#^' successfully stored in a file...

After successfully creating the byte_list, we imported the struct library.

Python’s struct module is a collection of functions that are used with binary data to perform operations such as packing, unpacking, and analyzing the contents of structs like C-structure formats. We imported it to process the byte_list.

The library provides the struct.pack() function that converts regular Python data types into packed binary data. It holds two arguments, as listed below.

  • fmt – specifies the format in which the objects are to be packed.
  • vals – contains any number of parameters needed to create the binary representation required for each element specified in the fmt parameter.

We used the 4B format to convert the byte_list into packed byte_array of the bytes data type. We used try-except blocks to handle the errors properly. Finally, we used the open() and write() functions to open the file and write the array to the byte_array.txt file.

Use encode() Function with join()

To write a binary file in Python:

  • Use the join() function to combine list elements as char.
  • Use the encode() function to convert the string of bytes to a bytes type object.
  • Use a with clause with open() the method in write binary mode(wb)
  • Use write() method to write the byte_array to the file.
byte_list = [100, 56, 35, 94]
byte_array = ''.join(chr(byte) for byte in byte_list).encode('charmap')
try:
    with open("bytes_array.txt", 'wb') as f:
        f.write(byte_array)
        print(str(byte_array) + " successfully stored in a file...")
except Exception as e:
    print(e)
b'd8#^' successfully stored in a file...

The join() function in Python is a built-in method that combines multiple strings into one string. It takes an iterable object as an argument and returns a single string formed from the elements of the iterable object separated by a separator character(s). We used the join() function to join the elements of byte_list as char.

The encode() function represents data in an alternative form. It converts information from one format to another, such as text to binary or vice versa. The purpose of encoding is to make the data more secure and easier to access and store.

We applied the encode() function on byte_array to convert it into a bytes type object and then wrote the array to the file using the open() and write() functions wrapped under the with statement.

Use to_bytes() Function

To write a binary file in Python:

  • Use with keyword with the open() function to open the file.
  • Use the for loop to iterate every element in the list of bytes.
  • Use the to_bytes() function on every element to convert it to a byte.
  • Use the write() function to write the byte to the file.
byte_list = [100, 56, 35, 94]
try:
    with open("bytes_array.txt", 'wb') as f:
        for byte in byte_list:
            f.write(byte.to_bytes(1, byteorder='big'))
        print("Successfully stored in a file...")
except Exception as e:
    print(e)
Successfully stored in a file...

We created the byte_list of integers in the range(0-255). Then, after opening the file using the open() function, we used the for loop to iterate every element of byte_list.

Python’s to_bytes() function provides a convenient way to convert various built-in data types, such as integers, into an equivalent representation in bytes of specified size. It enables the user to easily store or manipulate data represented as a sequence of bytes. The function holds arguments:

  • The length to ensure that the result has enough bytes to represent the number without data loss.
  • The byteorder specifies an endian encoding that represents the integer.

We applied the to_bytes() function over every element of byte_list and wrote the returned byte to the file.

]]>
https://java2blog.com/write-binary-file-python/feed/ 0
Remove Empty Lines from Text File in Python https://java2blog.com/remove-empty-lines-from-text-file-python/?utm_source=rss&utm_medium=rss&utm_campaign=remove-empty-lines-from-text-file-python https://java2blog.com/remove-empty-lines-from-text-file-python/#respond Sat, 26 Nov 2022 05:31:25 +0000 https://java2blog.com/?p=21334 Use isspace() Method

To remove empty lines from a Text File in Python:

  • Open a file in read and write mode(r+).
  • Use for loop for iterate over each line in the file.
  • Use isspace() method to check if line is empty. if it is not empty, add it to result.
  • Use seek(0) to move cursor to the start of the file.
  • Use write() method to write the result to the file.
result = ""
with open("./test.txt", "r+") as file:
    for line in file:
        if not line.isspace():
            result += line

    file.seek(0)  
    file.write(result)

Code assumes that you have test.txt in current directory with following content.

It's line one.
It's line two.

It's line three.

It's line four.

It's line five.
It's line six.

When you execute above program, you will get following result:

It's line one.
It's line two.
It's line three.
It's line four.
It's line five.
It's line six.

We created a string type variable result and initialize it with an empty string.

Next, the with statement is used to open the test.txt file in read and write mode (r+). The r+ mode is used to open a file to read and write and will produce an I/O error if the specified text file is not found.

Then, a for loop is used to iterate over each line of the test.txt file.

On each iteration, check if the current line is empty or not using the isspace() method.

The isspace() returns True if the current line has all whitespaces; otherwise False.

We used the seek() function to change the current file position to 0 because we wanted to overwrite the file with the data without empty lines.

Finally, the write() function is used to write into the file.

Another way of achieving the same results is to use join(), which joins all non-empty lines as follows.

result = ""
with open("./test.txt", "r+") as file:
    result += "".join(line for line in file
                 if not line.isspace())
    file.seek(0) 
    file.write(result)
It's line one.
It's line two.
It's line three.
It's line four.
It's line five.
It's line six.

If we don’t want to replace the data in the original file (test.txt) then we can create another file (new_test.txt) using another with statement.

Here, the w mode overwrites the existing file (if any) and creates a new file to write if it is not already there.

result = ""
with open("./test.txt", "r") as file:
    for line in file:
        if not line.isspace():
            result += line

with open("./new_test.txt", "w") as file:
    file.write(result)
It's line one.
It's line two.
It's line three.
It's line four.
It's line five.
It's line six.

The above output will be saved in the new_test.txt file, the test.txt will remain unchanged. You can learn about different modes to read and write text files here.

Use strip() Method

The following code is similar to the above code examples except for one difference, here, we are using the strip() function which is used to remove trailing and leading whitespaces from the string.

To identify an empty line, we find the length of the line.strip() using the len() function and continue execution if it is 0; otherwise concatenate the line with result.

Finally, change the current file position to 0 to overwrite the file while the write() is used to write data to the file.

result = ""

with open("./test.txt","r+") as file:
    for line in file.readlines():
        if (len(line.strip()) == 0):
            continue
        if line:
            result += line  

    file.seek(0)
    file.write(result)
It's line one.
It's line two.
It's line three.
It's line four.
It's line five.
It's line six.

Till this point, we used the open() function to open a .txt file. What if we want to open multiple text files, remove empty lines and save the data without empty lines in a new text file?

For that, we will be using multiple open() functions as follows which will reduce the readability.

with open("./test1.txt","r+") as file1, open("./test2.txt","r+") as file2

To maintain code readability, we can use the fileinput module.

Use the FileInput() Method

Here, we import the fileinput module to use its FileInput() method. We can use it as the context manager in the with statement. The FileInput() takes the following three parameters:

  • files – It denotes a tuple of multiple text files that we want to read.
  • inplace – It is set to True then the file content will be moved to the backup file. If we don’t want to lose data from the .txt files then, it must be False.
  • backup – This parameter is given to specify the extension of the backup file.

After that, we use the for loop to iterate over the data of both files, remove empty lines and concatenate non-empty lines with the result variable.

The important point is that we add a new line when the pointer starts reading the second file, otherwise, the last line of the test1.txt file and the first line of the test2.txt file would be on the same line. Don’t be confused, we will test both scenarios below using code examples.

import fileinput

result=""

with fileinput.FileInput(
               files=("./test1.txt", "./test2.txt"),
               inplace = False, backup ='.bak')
               as file:

    for line in file:
        if (len(line.strip()) == 0):
                continue
        if line:
            result += line

with open("./two_files_data.txt", "w") as file:
     file.write(result)
It's line one.
It's line two.
It's line three.
It's line four.
It's line five.
It's line six.It's line seven.
It's line eight.
It's line nine.
It's line ten.

See, we have It's line six from test1.txt and It's line seven from test2.txt on the same line. To get rid of this, we will use the if condition to add a new line character if we are on the first line of the test2.txt file.

import fileinput

result=""

with fileinput.FileInput(
              files=("./test1.txt", "./test2.txt"),
              inplace = False, backup ='.bak')
              as file:

    for line in file:
        if (len(line.strip()) == 0):
                continue
        if line:
            if (file.filename() == "./test2.txt"
                and file.filelineno() == 1):
                result += '\n'
            result += line

with open("./two_files_data.txt", "w") as file:
     file.write(result)
It's line one.
It's line two.
It's line three.
It's line four.
It's line five.
It's line six.
It's line seven.
It's line eight.
It's line nine.
It's line ten.

That’s all about how to remove empty lines from text file in Python.

]]>
https://java2blog.com/remove-empty-lines-from-text-file-python/feed/ 0
Create Temp File in Python https://java2blog.com/create-temp-file-python/?utm_source=rss&utm_medium=rss&utm_campaign=create-temp-file-python https://java2blog.com/create-temp-file-python/#respond Wed, 16 Nov 2022 13:27:49 +0000 https://java2blog.com/?p=21169 Sometimes applications require storage and processing of temporary data. For such purposes, Python provides the tempfile library that can be used to create such files and directories.

How to Create Temp File in Python

This tutorial will demonstrate how to create temp file in Python.

Using the tempfile.NamedTemporaryFile object

The NamedTemporaryFile constructor is used to create objects that resemble a file-like object in the temporary storage area and can be used to read and write data. The file is created securely using some predefined rules and is destroyed as soon as it is closed. The file has a certain name in the system that can be retrieved with the name attribute.

We can open the file in the required mode using the mode parameter. Other parameters are also available like encoding, prefix, newline, and more.

The default value of the mode parameter is w+b which means we can read and write using the same object without closing it.

See the code below.

import tempfile
with tempfile.NamedTemporaryFile() as f:
    name = f.name
    f.write(b'Java2Blog') 
    f.seek(0) 
    data = f.read() 
    print(f"Directory: {name} Content: {data}")

Output:

Directory: C:\Users\AppData\Local\Temp\tmp76uo8lzt Content: b’Java2Blog’

In the above example, we create Temp file in Python and write data to the same. The name attribute is used to access the directory and name of the temporary file. The write() function is used to write data to this file.

Then we seek the file pointer to the starting position using the seek() method, read the data using the read() function, and display the same. Since we are using the with statement there is no need to use the close() function to destroy the object.

Using the tempfile.TemporaryFile object

The TemporaryFile object works similarly to the NamedTemporaryFile object but it does not explicitly return a file name. However, for platforms that nor PSOIX neither Cygwin, this is an alias for NamedTemporaryFile object.

We use it similarly to the previous function.

import tempfile
with tempfile.TemporaryFile() as f:
    f.write(b'Java2Blog') 
    f.seek(0) 
    data = f.read() 
    print(f"Content: {data}")

Output:

Content: b’Java2Blog’

Using the mkstemp() function

This function works similarly to the previous methods to create temp file in Python. It returns a tuple with the file object and the file name.

However, it does not provide functions to read or write data but we can use the methods from the os library. Also unlike the previous methods, we need to destroy the file explicitly after creating it.

See the code below.

import os
import tempfile
f, name = tempfile.mkstemp()
print(name)
os.write(f, b"Java2Blog")
os.close(f)

Output:

C:\Users\AppData\Local\Temp\tmpueokqk3y

Conclusion

To conclude this article, we discussed several methods to create temp file in Python. For this, we used the tempfile library that allows us to create temporary files and directories. T

TemporaryFile and NamedTemporaryFile objects work very similarly in allowing us to create file-like objects that create such temporary files and we can read-write data accordingly. The difference between the two lies in the fact that the latter returns an explicit name of the file that can be accessed using the name attribute.

We can also use the mkstemp() function that works similarly to these methods but we need to explicitly close and clean the temporary files.

]]>
https://java2blog.com/create-temp-file-python/feed/ 0
Get Temp Directory in Python https://java2blog.com/get-temp-directory-python/?utm_source=rss&utm_medium=rss&utm_campaign=get-temp-directory-python https://java2blog.com/get-temp-directory-python/#respond Sun, 02 Oct 2022 17:00:49 +0000 https://java2blog.com/?p=20832 Sometimes we can encounter scenarios where we wish to store temporary data in Python. This can include situations of creating temporary files and directories.

In this tutorial, we will discuss how to get temp directory in Python.

Get Temp Directory in Python

The tempfile module is used in Python to work with and create temporary files and directories. We will discuss different methods from this module to get temp directory in Python.

Using the tempfile.Gettempdir() Function to Get Temp Directory in Python

The gettempdir() function from the tempfile module is used to return the default temporary directory that is used by the user for working with temporary files. It follows a set of precedents to determine the temporary directory.

This directory can be interpreted from the environment variables. Usually, it is the directory named TMP, TEMP, or TMPDIR by the environment variable.

If the directory cannot be interpreted from the same then it may return the platform-specific directory. This can be like C:\TMP, C:\TEMP in the windows or like tmp, usr/tmp on other platforms.

If still the temporary directory is not determined, then the function will return the current working directory as the same.

For example,

import tempfile
path = tempfile.gettempdir()
print(path)

Output:

C:\tmp

The above example shows how to get temp directory in Python using the gettempdir() method. In case, you need to create temp file, you can follow how to create temp file in Python.

In Python 3.1 and above, the result is returned as a string. To get the value in bytes we can use the function discussed below.

Using the tempfile.Gettempdirb() Function to Get Temp Directory in Python

The gettempdirb() function from the tempfile module returns the temporary directory as bytes. That is the only distinction between the gettempdir() and gettempdirb() functions.

This can be useful in Python 3 which changed the encoding and introduced a distinction between strings and bytes.

For example,

import tempfile
path = tempfile.gettempdirb()
print(path)

Output:

b’C:\tmp’

The b prefix in the above example indicates that the final result is in the form of bytes.

Using the tempfile.Tempdir Variable to Get Temp Directory in Python

An important thing to remember is that both the above-mentioned methods use a pre-defined global variable to return the temp directory. This variable is called tempfile.tempdir. It is not recommended to set this variable manually and it is also not advised to set its value as bytes as it will alter the return type of functions like mkdtemp and mkstemp that create further temp directories and files.

We can view this variable directly as well after calling any of the above two functions.

For example,

import tempfile
path = tempfile.tempdir
print(path)

Output:

C:\tmp

Conclusion

To conclude, we discussed several methods of the tempfile module to get temp directory in Python. First, we discussed the use of the tempfile module. Then, we demonstrated the use of the gettempdir() and gettempdirb() functions to get temp directory in Python in different formats. We discussed the precedence of rules to determine the same and how they both use the global variable tempdir to return the value.

]]>
https://java2blog.com/get-temp-directory-python/feed/ 0
Remove Extension From Filename in Python https://java2blog.com/remove-extension-from-filename-python/?utm_source=rss&utm_medium=rss&utm_campaign=remove-extension-from-filename-python https://java2blog.com/remove-extension-from-filename-python/#respond Thu, 19 May 2022 09:33:52 +0000 https://java2blog.com/?p=19390 While programming in python, we often have to deal with file names. In this article, we will discuss how we can Remove Extension From Filename in python.  

How to Remove Extension From Filename in Python?

To Remove Extension From Filename in python, we can use the functions provided in the os module or the pathlib module. Let us discuss them one by one.  

Remove Extension From Filename in Python Using the os Module

Given a file name, we can remove the file extension using the os.path.splitext() function. The splitext() function takes the file name as its input argument and returns a tuple containing the file name as its first element and the file extension as its second argument. 

To remove the file extension, we can assign the first element of the tuple returned by the splitext() function to the variable containing the original filename as follows.

import os

filename = 'Demo.csv'
print("The original filename:", filename)
tempTuple = os.path.splitext(filename)
print("The tuple is:", tempTuple)
filename = tempTuple[0]
print("The output filename:", filename)

Output:

The original filename: Demo.csv
The tuple is: ('Demo', '.csv')
The output filename: Demo

Instead of the filename, when we pass the entire file path to the splitext() function, it returns the entire file path as the first element of the tuple as shown below. 

import os

filename = '/home/aditya1117/PycharmProjects/pythonProject/Demo.csv'
print("The original filename:", filename)
tempTuple = os.path.splitext(filename)
print("The tuple is:", tempTuple)
filename = tempTuple[0]
print("The output filename:", filename)

Output:

The original filename: /home/aditya1117/PycharmProjects/pythonProject/Demo.csv
The tuple is: ('/home/aditya1117/PycharmProjects/pythonProject/Demo', '.csv')
The output filename: /home/aditya1117/PycharmProjects/pythonProject/Demo

To remove the file type and get only the file name in such cases, we will first use the os.path.basename() to get the file name. The os.path.basename() function takes the string containing the file name as its input argument and returns the name of the file as shown below.

import os

filename = '/home/aditya1117/PycharmProjects/pythonProject/Demo.csv'
print("The original filename:", filename)
filename = os.path.basename(filename)
print("The output filename:", filename)

Output:

The original filename: /home/aditya1117/PycharmProjects/pythonProject/Demo.csv
The output filename: Demo.csv

After obtaining the filename, you can use the os.path.splitext() function to remove the file extension or file type from the filename in python as follows.

import os

filename = '/home/aditya1117/PycharmProjects/pythonProject/Demo.csv'
print("The original filename:", filename)
filename = os.path.basename(filename)
print("The modified filename:", filename)
tempTuple = os.path.splitext(filename)
print("The tuple is:", tempTuple)
filename = tempTuple[0]
print("The output filename:", filename)

Output:

The original filename: /home/aditya1117/PycharmProjects/pythonProject/Demo.csv
The modified filename: Demo.csv
The tuple is: ('Demo', '.csv')
The output filename: Demo

The os.path.splitext() method works only when the input file name has only one file extension as in ‘abc.txt’, ‘abc.pdf’, or ‘x/y/z/abc.txt’. If the file name or path has more than one extension as in ‘abc.tar.gz’, The os.path.splitext() method can only be used to remove the last extension of the file. You can observe this in the following example.

import os

filename = 'Demo.tar.gz'
print("The original filename:", filename)
tempTuple = os.path.splitext(filename)
print("The tuple is:", tempTuple)
filename = tempTuple[0]
print("The output filename:", filename)

Output:

The original filename: Demo.tar.gz
The tuple is: ('Demo.tar', '.gz')
The output filename: Demo.tar

In the output, you can observe that the splitext() method is only splitting the Demo.tar.gz into Demo.tar and ‘gz’. To remove the file extension of these types of file names, we can use an iterative method, which we have discussed in the coming sections. 

Remove Extension From Filename in Python using the pathlib module

To remove the file extension from a filename using the pathlib module, we will first create a path object using the Path() method. The Path() method takes a string containing the filename as an input argument and returns a POSIX path object in UNIX-based machines or ntpath object in Windows machines. After obtaining the path object, we can remove the file extension using the stem attribute of the path object as follows.

import pathlib

filename = pathlib.Path('Demo.csv')
print("The original filename:", filename)
filename = filename.stem
print("The output filename:", filename)

Output:

The original filename: Demo.csv
The output filename: Demo

In this approach, even if we pass the full path of the file instead of the filename, we get only the filename. You can observe this in the following example.

import pathlib

filename = pathlib.Path('/home/aditya1117/PycharmProjects/pythonProject/Demo.csv')
print("The original filename:", filename)
filename = filename.stem
print("The output filename:", filename)

Output:

The original filename: /home/aditya1117/PycharmProjects/pythonProject/Demo.csv
The output filename: Demo

Again, this approach works only when the input file name has only one file extension as in ‘abc.txt’, ‘abc.pdf’, or ‘x/y/z/abc.txt’. If the file name or path has more than one extension as in ‘abc.tar.gz’, The stem attribute can only be used to remove the last extension of the file. You can observe this in the following example.

import pathlib

filename = pathlib.Path('Demo.tar.gz')
print("The original filename:", filename)
filename = filename.stem
print("The output filename:", filename)

Output:

The original filename: Demo.tar.gz
The output filename: Demo.tar

In the output, you can observe that the stem attribute only returns Demo.tar instead of Demo, which is the actual filename. To remove the file extension of these types of file names, we can use an iterative method using the suffixes attribute as discussed below.

Remove Multiple File Extensions Using pathlib Module in Python

After creating the path object using the Path() function, we can access all the extensions of the file using the suffixes attribute as follows.

import pathlib

filename = pathlib.Path('Demo.tar.gz')
print("The original filename:", filename)
extensions=filename.suffixes
print("The file extensions are:",extensions)

Output:

The original filename: Demo.tar.gz
The file extensions are: ['.tar', '.gz']

Now that we have the total number of extensions in the file name, we can use a for loop to remove all the extensions from the file as follows.

import pathlib

filename = pathlib.Path('Demo.tar.gz')
print("The original filename:", filename)
extensions = filename.suffixes
print("The file extensions are:", extensions)
for extension in extensions:
    filename = filename.stem
    filename = pathlib.Path(filename)
print("The output filename is:", filename)

Output:

The original filename: Demo.tar.gz
The file extensions are: ['.tar', '.gz']
The output filename is: Demo

In the for loop, we need to convert the filename to a path object each time after extracting the filename using the stem attribute. Otherwise, the program will run into an error. This is due to the reason that the stem attribute contains the filename as a string. We cannot use the stem attribute on a string. Therefore, we need to convert the string into a path object using the Path() method, each time we obtain the filename in the for loop.

Conclusion

In this article, we have discussed different approaches to remove file extension or file type in python. I would suggest you use the approaches with the pathlib module as the pathlib module makes it easy to handle the file path.

I hope you enjoyed reading this article. Stay tuned for more informative articles.

Happy Learning

]]>
https://java2blog.com/remove-extension-from-filename-python/feed/ 0
Get Directory Name From File Path in Python https://java2blog.com/get-directory-name-from-file-path-python/?utm_source=rss&utm_medium=rss&utm_campaign=get-directory-name-from-file-path-python https://java2blog.com/get-directory-name-from-file-path-python/#respond Thu, 19 May 2022 07:05:43 +0000 https://java2blog.com/?p=19370 File operations are a fundamental part of python application development. In this article, we will discuss how we can get the directory name from the file path in python. Later, we will also discuss how we can remove filename from the file path in python.

How to Get directory name from file path in python?

Python provides us with the os module to perform file operations. Additionally, we can also use the pathlib module to get the directory from the file path in python. Let us discuss programs to get directory from file path using both the modules in python.

Get Directory Name From File Path Using the os Module in Python

The os.path module provides us with the dirname() method and the split() method with which we can get the directory from the file name in python. Let us discuss both methods one by one.

Get Directory Name From File Path Using the os.path.dirname() Function

The os.path.dirname() function takes a file path as its input argument and returns a string containing the directory. For instance, if you will provide the file path of a file, the dirname() function will return the directory containing the file as shown below.

import os

filepath = '/home/aditya1117/PycharmProjects/pythonProject/Demo.csv'
print("The file path is:")
print(filepath)
directoryName = os.path.dirname(filepath)
print("The directory is:")
print(directoryName)

Output:

The file path is:
/home/aditya1117/PycharmProjects/pythonProject/Demo.csv
The directory is:
/home/aditya1117/PycharmProjects/pythonProject

Similarly, if we pass the file path of a directory, the dirname() function will return the path of the directory containing the current directory. You can observe this in the following example.

import os

filepath = '/home/aditya1117/PycharmProjects/pythonProject'
print("The file path is:")
print(filepath)
directoryName = os.path.dirname(filepath)
print("The directory is:")
print(directoryName)

Output:

The file path is:
/home/aditya1117/PycharmProjects/pythonProject
The directory is:
/home/aditya1117/PycharmProjects

Get Directory Name From File Path Using the os.path.split() Function

We can also get the directory from the file path using the os.path.split() function. The split() function takes the file name as its input argument and returns a tuple containing two strings. The first string contains the file path of the parent directory of the current file or directory. The second element of the tuple contains the name of the current directory or file. 

If given the file path of a file, we can get the directory using the split() function from the first element of the tuple returned by the split() function as follows.

import os

filepath = '/home/aditya1117/PycharmProjects/pythonProject/Demo.csv'
print("The file path is:")
print(filepath)
nameTuple = os.path.split(filepath)
print("The tuple is:")
print(nameTuple)
directoryName = nameTuple[0]
print("The directory is:")
print(directoryName)

Output:

The file path is:
/home/aditya1117/PycharmProjects/pythonProject/Demo.csv
The tuple is:
('/home/aditya1117/PycharmProjects/pythonProject', 'Demo.csv')
The directory is:
/home/aditya1117/PycharmProjects/pythonProject

If we pass the file path of a directory to the split() function, we can get the name of the parent directory as follows.

import os

filepath = '/home/aditya1117/PycharmProjects/pythonProject'
print("The file path is:")
print(filepath)
nameTuple = os.path.split(filepath)
print("The tuple is:")
print(nameTuple)
directoryName = nameTuple[0]
print("The directory is:")
print(directoryName)

Output:

The file path is:
/home/aditya1117/PycharmProjects/pythonProject
The tuple is:
('/home/aditya1117/PycharmProjects', 'pythonProject')
The directory is:
/home/aditya1117/PycharmProjects

Get Directory Name From File Path Using the pathlib Module

Instead of using the os module, we can also use the pathlib module to get the directory from the file path in python. For this, we can use the Path() function. The Path() function takes the file path as its input argument and returns a POSIX path object in UNIX or ntpath object in NTFS (windows). 

To get the directory from the path object, we can use the parent attribute of the path object. The parent attribute of the path object contains the file path of the current directory. We can convert the POSIX or ntpath object containing the directory path in the parent attribute to a simple directory path using the str() method. In this way, we can get the directory from the file path using the pathlib module as follows.

import pathlib

filepath = '/home/aditya1117/PycharmProjects/pythonProject/Demo.csv'
print("The file path is:")
print(filepath)
pathObject = pathlib.Path(filepath)
directoryName = pathObject.parent
print("The directory is:")
print(directoryName)

Output:

The file path is:
/home/aditya1117/PycharmProjects/pythonProject/Demo.csv
The directory is:
/home/aditya1117/PycharmProjects/pythonProject

Similarly, we can get the parent directory of a directory using the pathlib module as follows.

import pathlib

filepath = '/home/aditya1117/PycharmProjects/pythonProject'
print("The file path is:")
print(filepath)
pathObject = pathlib.Path(filepath)
directoryName = pathObject.parent
print("The directory is:")
print(directoryName)

Output:

The file path is:
/home/aditya1117/PycharmProjects/pythonProject
The directory is:
/home/aditya1117/PycharmProjects

Remove FilnName from File Path

There are multiple ways to remove Filename from file path. Let’s go through them.

Remove Filename From the File Path Using the OS Module in Python

To remove the filename from the file path, we can use the os module and the pathlib module. 

To remove the filename from a file path using the os module, we will first get the directory from the file path using the os.path.dirnname() function. Then, we will assign the directory to the file path. In this way, we can remove the filename from the file path as shown below.

import os

filepath = '/home/aditya1117/PycharmProjects/pythonProject/Demo.csv'
print("The file path is:")
print(filepath)
filepath = os.path.dirname(filepath)
print("The new file path is:")
print(filepath)

Output:

The file path is:
/home/aditya1117/PycharmProjects/pythonProject/Demo.csv
The new file path is:
/home/aditya1117/PycharmProjects/pythonProject

In a similar manner, we can remove the filename from a file path using the os.path.split() function. Here, we will assign the first element of the tuple given as output by the split function i.e. the directory name to the variable containing the file path. In this way, we can remove the filename from the file path using the os.path.split() function as shown below.

import os

filepath = '/home/aditya1117/PycharmProjects/pythonProject/Demo.csv'
print("The file path is:")
print(filepath)
nameTuple = os.path.split(filepath)
print("The tuple is:")
print(nameTuple)
filepath = nameTuple[0]
print("The new file path is:")
print(filepath)

Output:

The file path is:
/home/aditya1117/PycharmProjects/pythonProject/Demo.csv
The tuple is:
('/home/aditya1117/PycharmProjects/pythonProject', 'Demo.csv')
The new file path is:
/home/aditya1117/PycharmProjects/pythonProject

Remove Filename From File Path Using the Pathlib Module in Python

To remove the filename from the file path using the pathlib module, we will first get the name of the parent directory using the parent attribute of the file path object returned by the Path() object. as discussed in the previous section. After that, we will assign the directory to the variable containing the original file path. In this way, we can remove the filename from the file path using the pathlib module as follows.

import pathlib

filepath = '/home/aditya1117/PycharmProjects/pythonProject/Demo.csv'
print("The file path is:")
print(filepath)
pathObject = pathlib.Path(filepath)
filepath = pathObject.parent
print("The new file path is:")
print(filepath)

Output:

The file path is:
/home/aditya1117/PycharmProjects/pythonProject/Demo.csv
The new file path is:
/home/aditya1117/PycharmProjects/pythonProject

Conclusion

In this article, we have discussed how to get the directory name from the file path in python using different approaches. We also saw how we can filename from the file path in python. All of the approaches are semantically similar and have almost the same time complexity. So, you can use any of the approaches to get the work done. 

I hope you enjoyed reading this article. Stay tuned for more informative articles. 

Happy Learning!

]]>
https://java2blog.com/get-directory-name-from-file-path-python/feed/ 0