501 views|6 replies

538

Posts

3

Resources
The OP
 

Reading notes on the book "Python Programming Quick Start (2nd Edition)" 5. File Operations (1) [Copy link]

This study document related operations.

1. Folder separator of the path

In Windows, path writing uses backslashes as separators between folders. However, in macOS and Linux operating systems, forward slashes are used to separate paths.

In order to uniformly handle the file path differences between different systems, Python provides a pathlib module (the Path() method in it) that can easily handle this problem.

The Path() method returns a string representing the file path.

2. Use the / operator to connect paths

Python can directly use / to connect paths, but the prerequisite is that the previous path uses the Path() method.

For example:

It is particularly important to note that when using the / operator to connect paths, the / must be preceded by a Path object.

3. Current working directory

Python can use the Path.cwd() function to get the string of the current working path, and can use the os.chdir() function to change it, as shown in the following example:

Alternatively, you can also use the os.getcwd() function to get the current working directory, but this is an older method.

4. Main Directory

Each user on the computer has his own folder, which is called the home directory. The home directory can be obtained through the Path.home() function.

5. Create a new folder

Python can create new folders using the os.makedirs() function.

This method can create multiple directories at one time.

There is more knowledge related to file operations, which I will continue to learn next time.

Latest reply

Python is indeed very convenient, mainly at the application level   Details Published on 2024-5-16 12:16
 
 

27

Posts

0

Resources
2
 

Brother, are you also learning? I have just started learning and I don’t know how long I can stick with it.

Comments

You need to find a way to apply this knowledge to actual projects, otherwise you won’t be able to remember it and it’s easy to give up.  Details Published on 2024-5-16 08:07
 
 
 

6818

Posts

11

Resources
3
 

There is more knowledge related to file operations, which I will continue to learn next time.

Building a Linux virtual machine makes learning more interesting.

Comments

Haha, Linux is all files, it's OK  Details Published on 2024-5-16 08:09
 
 
 

538

Posts

3

Resources
4
 
EFT published on 2024-5-16 00:09 Brother, are you also learning? I have just started learning and I don’t know how long I can stick to it

You need to find a way to apply this knowledge to actual projects, otherwise you won’t be able to remember it and it’s easy to give up.


 
 
 

538

Posts

3

Resources
5
 
lugl4313820 posted on 2024-5-16 07:39 There is more knowledge related to file operations, I will continue to learn next time. Building a Linux virtual machine will make learning more interesting.

Haha, Linux is all files, it's OK


 
 
 

145

Posts

0

Resources
6
 

Python is indeed very convenient, mainly at the application level

Comments

Uh-huh, yes  Details Published on 2024-5-17 16:36
 
 
 

538

Posts

3

Resources
7
 
qzc飘漾 posted on 2024-5-16 12:16 Python is indeed very convenient, mainly at the application level

Uh-huh, yes


 
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list