博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python列重命名_Python目录–创建,重命名,删除,列出,更改
阅读量:2535 次
发布时间:2019-05-11

本文共 5416 字,大约阅读时间需要 18 分钟。

python列重命名

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

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

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 importedprint(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 moduleprint(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 directoryos.chdir('/Users/pankaj/temp')#print current working directoryprint(os.getcwd())#create directoryos.mkdir("data")print(os.listdir(os.getcwd()))#rename directoryos.rename("data","data1")print(os.listdir(os.getcwd()))#delete directoryos.rmdir("data1")print(os.listdir(os.getcwd()))#delete non-empty directoryos.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 
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 shutilshutil.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:

参考: :

翻译自:

python列重命名

转载地址:http://gyqzd.baihongyu.com/

你可能感兴趣的文章
Spring - DI
查看>>
微软自己的官网介绍 SSL 参数相关
查看>>
Composite UI Application Block (CAB) 概念和术语
查看>>
ajax跨域,携带cookie
查看>>
阶段3 2.Spring_01.Spring框架简介_03.spring概述
查看>>
阶段3 2.Spring_02.程序间耦合_1 编写jdbc的工程代码用于分析程序的耦合
查看>>
阶段3 2.Spring_01.Spring框架简介_04.spring发展历程
查看>>
阶段3 2.Spring_02.程序间耦合_3 程序的耦合和解耦的思路分析1
查看>>
阶段3 2.Spring_02.程序间耦合_5 编写工厂类和配置文件
查看>>
阶段3 2.Spring_01.Spring框架简介_05.spring的优势
查看>>
阶段3 2.Spring_02.程序间耦合_7 分析工厂模式中的问题并改造
查看>>
阶段3 2.Spring_02.程序间耦合_4 曾经代码中的问题分析
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_2 spring中的Ioc前期准备
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_4 ApplicationContext的三个实现类
查看>>
阶段3 2.Spring_02.程序间耦合_8 工厂模式解耦的升级版
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_6 spring中bean的细节之三种创建Bean对象的方式
查看>>
阶段3 2.Spring_04.Spring的常用注解_3 用于创建的Component注解
查看>>
阶段3 2.Spring_04.Spring的常用注解_2 常用IOC注解按照作用分类
查看>>
阶段3 2.Spring_09.JdbcTemplate的基本使用_5 JdbcTemplate在spring的ioc中使用
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_02.ssm整合之搭建环境
查看>>