Home Backend Development Python Tutorial Examples of file name and file path operations in python

Examples of file name and file path operations in python

Oct 03, 2017 am 05:41 AM
python file name path

The following editor will bring you an example of python file name and file path operation. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look.

Readme:

In daily work, we often involve operations related to file names and file paths. In python, os The standard module provides us with various functions for file operations. This article will introduce respectively "get the current path", "get all files and folders under the current path", "delete files", "delete directories/multiple directories", "check File/File Path", "Check if file path exists", "Separate file path and file name", "Separate file extension", "Get file name" and Get "file path".


import os
'''获得当前路径
'''
cwd=os.getcwd()
print(cwd)
Copy after login


'''
得到当前文件夹下的所有文件和文件夹
'''
print(os.listdir())
Copy after login


'''
delete file
'''
os.remove('sw724.vaps')
print(os.listdir())
Copy after login


'''
删除单个目录和多个目录
'''
os.removedir()
os.removedir()
Copy after login


'''
检查是否是文件/文件夹
'''
print(os.path.isfile('/Users/liuxiaolong/PycharmProjects/untitled/sw724.vaps'))
print(os.path.isdir('/Users/liuxiaolong/PycharmProjects/untitled/sw724.vaps'))
Copy after login


'''
检查文件路径是否存在
'''

print(os.path.exists('/Users/liuxiaolong/PycharmProjects/untitled/iiii'))
Copy after login


'''
分离文件名
分离扩展名

'''
[dirname,filename]=os.path.split('/Users/liuxiaolong/PycharmProjects/untitled/sw724.vaps')
print(dirname,"\n",filename)

[fname,fename]=os.path.splitext('/Users/liuxiaolong/PycharmProjects/untitled/sw724.vaps')
print(fname,"\n",fename)
Copy after login


'''
获得文件路径
获得文件名
获得当前环境
'''
print("get pathname:",os.path.dirname('/Users/liuxiaolong/PycharmProjects/untitled/sw724.vaps'))
print("get filename:",os.path.basename('/Users/liuxiaolong/PycharmProjects/untitled/sw724.vaps'))
print(os.getenv)
Copy after login

The above is the detailed content of Examples of file name and file path operations in python. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to download deepseek Xiaomi How to download deepseek Xiaomi Feb 19, 2025 pm 05:27 PM

How to download deepseek Xiaomi

What are the advantages and disadvantages of templating? What are the advantages and disadvantages of templating? May 08, 2024 pm 03:51 PM

What are the advantages and disadvantages of templating?

Google AI announces Gemini 1.5 Pro and Gemma 2 for developers Google AI announces Gemini 1.5 Pro and Gemma 2 for developers Jul 01, 2024 am 07:22 AM

Google AI announces Gemini 1.5 Pro and Gemma 2 for developers

For only $250, Hugging Face's technical director teaches you how to fine-tune Llama 3 step by step For only $250, Hugging Face's technical director teaches you how to fine-tune Llama 3 step by step May 06, 2024 pm 03:52 PM

For only $250, Hugging Face's technical director teaches you how to fine-tune Llama 3 step by step

Share several .NET open source AI and LLM related project frameworks Share several .NET open source AI and LLM related project frameworks May 06, 2024 pm 04:43 PM

Share several .NET open source AI and LLM related project frameworks

A complete guide to golang function debugging and analysis A complete guide to golang function debugging and analysis May 06, 2024 pm 02:00 PM

A complete guide to golang function debugging and analysis

How do you ask him deepseek How do you ask him deepseek Feb 19, 2025 pm 04:42 PM

How do you ask him deepseek

How to save the evaluate function How to save the evaluate function May 07, 2024 am 01:09 AM

How to save the evaluate function

See all articles