Categories
Python Answers

How to strip punctuation from a string with Python?

Sometimes, we want to strip punctuation from a string with Python.

In this article, we’ll look at how to strip punctuation from a string with Python.

How to strip punctuation from a string with Python?

To strip punctuation from a string with Python, we can use the string’s translate method.

For instance, we can write:

import string

s = 'hello world!!!'
new_s = s.translate(str.maketrans('', '', string.punctuation))
print(new_s)

to call s.translate with str.maketrans('', '', string.punctuation) to return a new string without the punctuation characters in it.

Then we assign that to new_s.

Therefore, new_s is 'hello world'.

Conclusion

To strip punctuation from a string with Python, we can use the string’s translate method.

Categories
Python Answers

How to get the full path of the current file’s directory with Python?

Sometimes, we want to get the full path of the current file’s directory with Python.

In this article, we’ll look at how to get the full path of the current file’s directory with Python.

How to get the full path of the current file’s directory with Python?

To get the full path of the current file’s directory with Python, we can use the pathlib module.

For instance, we write:

import pathlib

curr_path = pathlib.Path(__file__).parent.resolve()
print(curr_path)

We use the pathlib.Path constructor with __file__ to create a Path instance from the current script file.

Then we call parent.resolve to return the current path of the file and assign that to curr_path.

Therefore, curr_path is something like '/home/runner/GrandPoorField'.

Conclusion

To get the full path of the current file’s directory with Python, we can use the pathlib module.

Categories
Python Answers

How to get file creation and modification date and times in Python?

Sometimes, we want to get file creation and modification date and times in Python.

In this article, we’ll look at how to get file creation and modification date and times in Python.

How to get file creation and modification date and times in Python?

To get file creation and modification date and times in Python, we can use the os.path.getctime and os.path.getmtime methods respectively.

For instance, we write:

import os.path, time

file = './bar.txt'
print("last modified: %s" % time.ctime(os.path.getmtime(file)))
print("created: %s" % time.ctime(os.path.getctime(file)))

We call time.ctime with os.path.getmtime to convert the Unix timestamp of the modification date and time of the file to a human readable date and time string.

Likewise, we call time.ctime with os.path.getctime to convert the Unix timestamp of the creation date and time of the file to a human readable date and time string.

Therefore, we should see something like:

last modified: Sun Oct 17 17:45:30 2021
created: Sun Oct 17 17:50:44 2021

printed from the print calls.

Conclusion

To get file creation and modification date and times in Python, we can use the os.path.getctime and os.path.getmtime methods respectively.

Categories
Python Answers

How to represent an enum in Python?

Sometimes, we want to represent an enum in Python.

In this article, we’ll look at how to represent an enum in Python.

How to represent an enum in Python?

To represent an enum in Python, we can use the enum module.

For instance, we write:

from enum import Enum


class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4


print(Animal.ant)

We create the Animal class that inherits from the Enum class.

And we define the enum attributes inside the Animal class.

Therefore, Animal.ant is printed from the print function.

Likewise, we can define an enum with the Enum class directly by writing:

from enum import Enum

Animal = Enum('Animal', 'ant bee cat dog')

print(Animal.ant)

We instantiate Enum with the name of the enum and the enum attributes separated by spaces in a string.

So Animal.ant is printed from the print function.

Conclusion

To represent an enum in Python, we can use the enum module.

Categories
Python Answers

How to move a file in Python?

Sometimes, we want to move a file in Python.

In this article, we’ll look at how to move a file in Python.

How to move a file in Python?

To move a file in Python, we can use the os.rename, os.replace or shutil.move methods.

They both take the source and destination path strings as arguments respectively.

For instance, we write:

import os

os.rename("./foo.txt", "./bar.txt")

to move the file from ./foo.txt to ./bar.txt.

Likewise, we write:

import os

os.replace("./foo.txt", "./bar.txt")

or

import shutil

shutil.move("./foo.txt", "./bar.txt")

to do the same thing.

Conclusion

To move a file in Python, we can use the os.rename, os.replace or shutil.move methods.

They both take the source and destination path strings as arguments respectively.