SoFunction
Updated on 2024-11-14

Summary of common operations on file paths in Python

1 File path

In data processing, often use the code to read the data in the file, then the first thing you need to know the file path of the file. File path is simply the location of the file. File path is divided into two pieces: folder path and file name, file name is divided into the basic name of the file and the extension.

Examples:

'/Users/Desktop/'

Among them.

'/Users/Desktop/' is the folder path.

'Data' is the base name of the file

'.csv' is the file extension.

2 Common Operations

2.1 File path creation

Key function: makedirs() - create new folder (directory)

import os
path = "/Users/Desktop/python_code/My_project/get_path/new"
(path)

2.2 File path acquisition

There are several scenarios to get the file path: 1, get the current working path; 2, get other file paths, other file paths can be one or more.

2.2.1 Getting the current working path

Key function: getcwd()

# Get current working directory
import os
path = () # Get current working directory folder path
name = 'data' # file name
expanded_name = '.csv' # file extension
file_path = (path,name+expanded_name)#File Path Splicing
print(file_path)

The results of the run are as follows:

/Users/Desktop/python_code/My_project/get_path/

2.2.2 Getting a file path

There are two ways to get the paths of other files, one is to enter them directly inside the code, and the other is to use a pop-up box to manually select the folder or file.

① Input the path directly into the code

# Get a file - enter the file path
path = '/Users/Desktop/python_code/My_project/get_path' # Input folder path
name = 'data' # file name
expanded_name = '.csv' # file extension
file_path = (path,name+expanded_name) #Path splicing
print(file_path)

The results of the run are as follows:

/Users/Desktop/python_code/My_project/get_path/

② Pop-up box to select a folder or file

Pop-up box to select a folder:

#Popup box to select folder
import os
import tkinter as tk
from tkinter import filedialog
window = ()
('Please select folder')
file_path = () # Select the destination folder
name = 'data' # file name
expanded_name = '.csv' # file extension
file_path = (file_path,name+expanded_name)
print(file_path)

A pop-up box selects the file:

#Popup box to select the target file
import tkinter as tk
from tkinter import filedialog
window = ()
('Please select the target file')
file_path = () #Select the target file
print(file_path)

2.2.3 Getting multiple file paths

In practice, more often than not, you need to get multiple file paths, the key to this is to get all the files in the folder and then process them one by one.

Method I:

import os
from glob import glob
path = '/Users/Desktop/python_code/My_project/get_path' # Folder path, type it in or get it as a popup box
expanded_name = '.csv' # file extension
file_paths = glob((path,'*' + expanded_name + '*')) # Get all .csv files
file_paths.sort() # Sort by file name
for i in range(len(file_paths)):
    print(file_paths[i])

Method II:

import os
path = '/Users/Desktop/python_code/My_project/get_path' # Folder path, get it by typing or popup box
expanded_name = '.csv' # file extension
all_file_names = (path) # Show all filenames
file_paths = []
for i in range(len(all_file_names)):
    if expanded_name in all_file_names[i]:
        file_paths.append((path,all_file_names[i])) # Filter out files with target extensions
file_paths.sort() # Sort by file name
for i in range(len(file_paths)):
    print(file_paths[i])

The results of the run are as follows:

/Users/Desktop/python_code/My_project/get_path/
/Users/Desktop/python_code/My_project/get_path/
/Users/Desktop/python_code/My_project/get_path/

2.3 File path processing

Commonly used file path processing are splicing and splitting.

2.3.1 File path stitching

Key functions: ()

import os
path = '/Users/Desktop/python_code/My_project/get_path' # Enter the folder path
name = 'data' # file name
expanded_name = '.csv' # file extension
file_path = (path,name+expanded_name)
print(file_path)

The results of the run are as follows:

/Users/Desktop/python_code/My_project/get_path/

2.3.2 File path splitting

① Split into folder path and file name

Key function: ()

import os
path = '/Users/Desktop/python_code/My_project/get_path' # Enter the folder path
name = 'data' # file name
expanded_name = '.csv' # file extension
file_path = (path,name+expanded_name)
#Split
result = (file_path) # Split into folder paths and file names
print(result)

The results of the run are as follows:

('/Users/Desktop/python_code/My_project/get_path', '')

② Split into file path and extension

Key function: ()

import os
path = '/Users/Desktop/python_code/My_project/get_path' # Enter the folder path
name = 'data' # file name
expanded_name = '.csv' # file extension
file_path = (path,name+expanded_name)
#Split
result = (file_path) # Split into file path and file extension
print(result)

The results of the run are as follows:

('/Users/Desktop/python_code/My_project/get_path/data', '.csv')

③ Get the file name (with extension)

Key function: ()

result = (file_path) # Get the file name
print(result)

Run results:

④ Get folder path

Key function: ()

result = (file_path) # Get the file name
print(result)

Run results:

/Users/Desktop/python_code/My_project/get_path

2.4 File path judgment

2.4.1 Determining whether a path exists

import os
# Determine if a path exists
path = () # Get current working directory folder path
name = 'data' # file name
expanded_name = '.csv' # file extension
file_path = (path,name+expanded_name)
judge = (file_path)
print(judge )

Run results:

True

2.4.2 Determining whether a folder is a path or not

import os
# Determine if a path exists
path = () # Get current working directory folder path
name = 'data' # file name
expanded_name = '.csv' # file extension
file_path = (path,name+expanded_name)
# Determine if it is a folder path
judge = (file_path)
print(judge)

Running results:

False

2.4.3 Determining if it is a file path

import os
# Determine if a path exists
path = () # Get current working directory folder path
name = 'data' # file name
expanded_name = '.csv' # file extension
file_path = (path,name+expanded_name)
# Determine if it is a file
judge = (file_path)
print(judge)

Running results:

True

Notes:

Folder path: /Users/Desktop/python_code/My_project/get_path

File path: /Users/Desktop/python_code/My_project/get_path/

The above is a summary of common operations in Python file path details, more information about Python file path operations please pay attention to my other related articles!