486 views|5 replies

91

Posts

0

Resources
The OP
 

"Python Programming Quick Start" Part 8: Understanding Python's reading and writing files [Copy link]

1. Some basic knowledge

1.1, What are absolute paths and relative paths?

"Absolute path", always starts from the root folder.

"Relative path", relative to the program's current working directory.

1.2, Verify the effectiveness of the path

If the path exists, calling the exists() method returns True, otherwise it returns False;

If the path exists and is a file, calling the is_file() method returns True, otherwise it returns False;

If the path exists and is a folder, calling the is_dir() method returns True, otherwise it returns False;

The operation examples are as follows:

"""
    绝对路径和相对路径
"""

from pathlib import Path

# 验证路径是否存在
a = Path('D:/PyCharm')
print(a.exists())

# 验证路径是否存在,并是一个文件
b = Path('D:/PyCharm/project/pythonProject/test/python_test.py')
print(b.is_file())

# 验证路径是否存在,并是一个文件夹
c = Path('D:/PyCharm/project/pythonProject/test')
print(c.is_dir())


#结果如下
#True
#True
#True

2: Read and write files

In Python, reading and writing files is a 3-step process:
2.1, call the open() function and return a File object.
2.2, call the read() or write() method of the File object.
3.3, Call the close() method of the File object to close the file.
The operation examples are as follows:
"""
    文件写入和读出操作
"""
# 打开文件 w 写入,文件如果不存在,会自动创建
f = open("E:/Project/python/文件.txt", "w", encoding="UTF-8")

# write方法写入,内容是写入内存缓存区
f.write("学python,不后悔!")

# flush刷新,写入到磁盘
f.flush()

# close关闭文件IO
f.close()

# 打开文件 r 只读模式 要求文件必须存在
f = open("E:/Project/python/文件.txt", "r", encoding="UTF-8")

# read方法读出
a = f.read(12)
print("a =", a)

# close关闭文件IO
f.close()


#结果如下
#a = 学python,不后悔!

Latest reply

This post was last edited by hellokitty_bean on 2024-5-20 10:30 By the way, the current Python version does not distinguish between OS path slashes? I just tried it and it passed. Indeed. Dear two masters, please give me more advice!   Details Published on 2024-5-20 10:29
 
 

6841

Posts

11

Resources
2
 
f.flush is very important, otherwise writing may fail sometimes.

Comments

Indeed. Boss, please give me more advice!  Details Published on 2024-5-20 08:08
 
 
 

91

Posts

0

Resources
3
 
lugl4313820 posted on 2024-5-20 07:00 f.flush is very important, otherwise sometimes you may not be able to write successfully.

Indeed. Boss, please give me more advice!


 
 
 

1129

Posts

1

Resources
4
 
This post was last edited by hellokitty_bean on 2024-5-20 10:30

By the way, the current Python version does not distinguish between OS path slashes? I just tried it and it passed.

Indeed. Dear two masters, please give me more advice!

Comments

Learn together and make progress together  Details Published on 2024-5-20 10:50
 
 
 

91

Posts

0

Resources
5
 
hellokitty_bean posted on 2024-5-20 10:29 By the way, the current Python version does not distinguish between OS path slashes? I just tried it, Pass. Indeed. Two masters, please give me more advice!

Learn together and make progress together

 
 
 

91

Posts

0

Resources
6
 
#Additional mode writes data, and the operation is supplemented as follows:
# Open file a in append mode and write to the file. If the file does not exist, it will be created automatically.
f = open("E:/Project/python/file.txt", "a", encoding="UTF-8")

# The write method writes the content to the memory buffer
f.write("\n123456")

# flush refresh, write to disk
f.flush()

# close closes the file IO
f.close()

# Open the file r read-only mode requires the file to exist
f = open("E:/Project/python/file.txt", "r", encoding="UTF-8")

# read method reads out
a = f.read(19)
print("a =", a)

# close closes the file IO
f.close()

 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

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