C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Tip: This method can be used to avoid IOErrors that occur when a program tries to open a file that does not exist.
IOErrorPython program that uses exists
from os import path
# See if this file exists.
# ... Write a custom message based on the result.
if path.exists("/enable1.txt"):
print("File exists")
else:
print("File not found, sorry")
Output
File exists
Bytes: The path.getsize method returns a numeric figure. This is the byte count of the target file.
Note: The returned value is in bytes, not megabytes. To change the number to megabytes, we would need to divide it.
Numbers, DividePython program that uses getsize
from os import path
# Get size of this text file.
size = path.getsize("/enable1.txt")
# Display the size in bytes.
print(size)
Output
1916146
Warning: Commonprefix does no validation—the prefix may not be a valid path. Be careful with invalid data.
Here: We create an empty list and then append three paths two it. Then, the commonprefix method returns the longest prefix.
Note: It returns the name of the enclosing folder of all the files. It omits the nested folder.
Python program that uses commonprefix
from os import path
# Add paths to a list.
paths = []
paths.append("C:\directory\name")
paths.append("C:\directory\image")
paths.append("C:\directory\folder\index")
# Get common prefix of the paths.
prefix = path.commonprefix(paths)
print(prefix)
Output
C:\directory
Tip: There are just file names, not paths. To open these files, we must combine the file name with the path.
Python program that lists files in folder
import os
# Get all files in this directory.
list = os.listdir("C:\\programs\\")
print(list)
Output
['file.php', 'file.pl', 'file.py', 'file.rb']
Tip: We access the join method by using its complete, composite name os.path.join. This requires no "from" in the import directive.
Python program that uses join
import os
# Join directory name not including slash.
result = os.path.join("C:\\programs", "file.py")
print(result)
# Join directory name with trailing slash.
result = os.path.join("C:\\programs\\", "file.py")
print(result)
Output
C:\programs\file.py
C:\programs\file.py
First part: The first part of the returned tuple is the folder path with no file name on it. The end slash is omitted.
Second part: This is the file name. So it will be something like file.txt. It includes the extension.
Raw literals: For file paths, please consider using raw string literals with an "r" prefix (unlike many of the examples on this page).
Python program that uses path.split
from os import path
example = r"C:\programs\file.txt"
# Use split on path.
result = path.split(example)
# The result has two parts.
print("Result:", result)
# The second part of the tuple is the file name.
print("File name:", result[1])
Output
Result: ('C:\\programs', 'file.txt')
File name: file.txt
Tip: We get path timestamps, and convert them to dates. Please look for the example "File timestamps" in the Datetime page.
Datetime