python列重命名_Python目录–创建,重命名,删除,列出,更改

python列重命名

Good day, learners! In this tutorial we are going to learn about Python Directory. In our previous tutorial, we learned about Python File.

祝您学习愉快! 在本教程中,我们将学习Python目录。 在上一教程中,我们了解了Python File

Python目录 (Python Directory)

Python Directory is basically working with operating system directories. To work with Python Directories, we need to import os module. This os modules contains functions to create, view, delete, rename directories.

Python目录基本上可以与操作系统目录一起使用。 要使用Python目录,我们需要导入os模块。 该操作系统模块包含用于创建,查看,删除,重命名目录的功能。

Basically, there are some common functions to access Python Directories. The functions are given below

基本上,有一些通用功能可以访问Python目录。 功能如下

  1. getcwd(): This function returns a string containing current directory from where to code is being executed.

    getcwd():此函数返回一个字符串,其中包含要执行代码的当前目录。
  2. listdir(location): This function returns a list string containing current the names of current directories.

    listdir(location):此函数返回包含当前目录名称的列表字符串。
  3. chdir(location): This function changes the current directory to the given location

    chdir(location):此函数将当前目录更改为给定位置
  4. mkdir(name): This function creates a new directory labeling with the given name.

    mkdir(name):此函数创建一个具有给定名称的新目录标签。
  5. rename(old_name,new_name): This function renames the old_name directory to new_name directory

    重命名(old_name,new_name):此函数将old_name目录重命名为new_name目录

Using these functions, you can do basic operation with python directories. But, to show you example, some sample code will be given to help you understand about the implementation.

使用这些功能,您可以对python目录执行基本操作。 但是,为了展示示例,将提供一些示例代码来帮助您了解实现。

获取目录列表 (Getting List of Directories)

You can get the list of directories on a specific location. To do so, you have to to use listdir(location) function. If you put the location, the function will return a list of string containing the names of the directories of the given location. The following code will help you understand the thing

您可以获取特定位置的目录列表。 为此,您必须使用listdir(location)函数。 如果放置该位置,该函数将返回一个字符串列表,其中包含给定位置的目录名称。 以下代码将帮助您理解

import os #the os module need to be imported

print(os.listdir('/usr')) #here the location is ‘/usr’

The output of the following code will be:

以下代码的输出将是:

================== RESTART: /home/imtiaz/directory.py ==================
/home/imtiaz
['locale', 'sbin', 'local', 'games', 'lib', 'share', 'lib32', 'src', 'include', 'bin']
>>>

Which is same as the actual picture.

与实际图片相同。

获取当前目录的位置 (Getting The Location of Current Directory)

As we said earlier, you can get the location of the current directory you’re in by using getcwd() function. The following code will illustrate you the idea

如前所述,您可以使用getcwd()函数获取当前目录的位置。 以下代码将说明您的想法

import os #we need to import this module

print(os.getcwd()) #print the current location

The output of the following code will be

以下代码的输出将是

================== RESTART: /home/imtiaz/cur_dir.py ==================
/home/imtiaz
>>>

Similarly, you can implement all those functions that are mentioned above. Try those, give yourself a challenge!

同样,您可以实现上述所有这些功能。 试试看,给自己一个挑战!

为什么我们需要Python目录 (Why Do We Need Python Directories)

Reading this tutorial, it may come to your mind that why we need Python Directories. In this section we will going to discuss this thing.

阅读本教程,您可能会想到为什么我们需要Python目录。 在本节中,我们将讨论这个问题。

Suppose, you are making some a software using Python where you need to read/write files from different directories. The directories can be dynamic so that you cannot fix the directory from your code, rather you need to choose the directory dynamically. After choosing the directory, you may have to create a new directory or write a file or read a file from that directory. To do so, Python has introduced this facility. You may not need this now, but Python Directory may help you later.

假设您正在使用Python开发一些软件,需要从不同目录读取/写入文件。 目录可以是动态的,因此您无法从代码中修复目录,而是需要动态选择目录。 选择目录后,您可能必须创建一个新目录或写入文件或从该目录读取文件。 为此,Python引入了此功能。 您现在可能不需要此功能,但是Python目录稍后可能会为您提供帮助。

Python创建重命名删除目录示例 (Python Create Rename Delete Directory Example)

A simple program showing how to create directory, then rename and delete it.

一个简单的程序,显示如何创建目录,然后重命名和删除它。

import os

#change directory
os.chdir('/Users/pankaj/temp')

#print current working directory
print(os.getcwd())

#create directory
os.mkdir("data")
print(os.listdir(os.getcwd()))

#rename directory
os.rename("data","data1")
print(os.listdir(os.getcwd()))

#delete directory
os.rmdir("data1")
print(os.listdir(os.getcwd()))

#delete non-empty directory
os.rmdir("test")
print(os.listdir(os.getcwd()))

When we execute above program through terminal, it produces following output.

当我们通过终端执行上述程序时,将产生以下输出。

pankaj:BasicPython pankaj$ python directory.py 
/Users/pankaj/temp
['data', 'test']
['data1', 'test']
['test']
Traceback (most recent call last):
  File "directory.py", line 22, in <module>
    os.rmdir("test")
OSError: [Errno 66] Directory not empty: 'test'

Notice that os.rmdir can only remove empty directory. So to delete a non-empty directory, we will have to use shutil module. Below is a simple program to delete directory using shutil module.

请注意, os.rmdir只能删除空目录。 因此,要删除一个非空目录,我们将不得不使用shutil模块。 下面是一个使用shutil模块删除目录的简单程序。

import shutil

shutil.rmtree('/Users/pankaj/temp/test')

So that’s all for python directory tutorial. Hope that you have learned well. Try to implement functions that are not implemented here. That’s you task! If you face any problem regarding this, feel free to use the comment section.

这就是python目录教程的全部内容。 希望你学得很好。 尝试实现此处未实现的功能。 那是你的任务! 如果您对此有任何疑问,请随时使用评论部分。

Reference: https://docs.python.org/3.6/library/shutil.html#shutil.rmtree

参考: https : //docs.python.org/3.6/library/shutil.html#shutil.rmtree

翻译自: https://www.journaldev.com/14417/python-directory-create-rename-delete

python列重命名

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值