Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
python-study/01python-base/File.py at master · nebofeng/python-study · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
See all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
nebofeng
python-study
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
python-study
/
01python-base
/
File.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
143 lines (124 loc) · 6.71 KB
master
Breadcrumbs
python-study
/
01python-base
/
File.py
Copy path
Top
File metadata and controls
Code
Blame
143 lines (124 loc) · 6.71 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#coding=utf-8
#输出
#读取键盘输入 raw_input input
# str = raw_input("请输入")
# print "你的内容是:" ,str
#input 与 rawinput() 基本类似,但是input可以接收一个Python表达式作为输入,并将运算结果返回。
# str1=input("请输入:")
# print "你输入的内容是:",str1
#打开关闭 文件。
# open 函数
# 你必须先用Python内置的open()函数打开一个文件,创建一个file对象,相关的方法才可以调用它进行读写。
#
# 语法:
#
# file object = open(file_name [, access_mode][, buffering])
# file_name:file_name变量是一个包含了你要访问的文件名称的字符串值。
# access_mode:access_mode决定了打开文件的模式:只读,写入,追加等。所有可取值见如下的完全列表。这个参数是非强制的,默认文件访问模式为只读(r)。
# buffering:如果buffering的值被设为0,就不会有寄存。如果buffering的值取1,访问文件时会寄存行。如果将buffering的值设为大于1的整数,
# 表明了这就是的寄存区的缓冲大小。如果取负值,寄存区的缓冲大小则为系统默认。
# 不同模式打开文件的完全列表:
#
# 模式 描述
# r 以只读方式打开文件。文件的指针将会放在文件的开头。这是默认模式。
# rb 以二进制格式打开一个文件用于只读。文件指针将会放在文件的开头。这是默认模式。一般用于非文本文件如图片等。
# r+ 打开一个文件用于读写。文件指针将会放在文件的开头。
# rb+ 以二进制格式打开一个文件用于读写。文件指针将会放在文件的开头。一般用于非文本文件如图片等。
# w 打开一个文件只用于写入。如果该文件已存在则打开文件,并从开头开始编辑,即原有内容会被删除。如果该文件不存在,创建新文件。
# wb 以二进制格式打开一个文件只用于写入。如果该文件已存在则打开文件,并从开头开始编辑,即原有内容会被删除。如果该文件不存在,创建新文件。一般用于非文本文件如图片等。
# w+ 打开一个文件用于读写。如果该文件已存在则打开文件,并从开头开始编辑,即原有内容会被删除。如果该文件不存在,创建新文件。
# wb+ 以二进制格式打开一个文件用于读写。如果该文件已存在则打开文件,并从开头开始编辑,即原有内容会被删除。如果该文件不存在,创建新文件。一般用于非文本文件如图片等。
# a 打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。也就是说,新的内容将会被写入到已有内容之后。如果该文件不存在,创建新文件进行写入。
# ab 以二进制格式打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。也就是说,新的内容将会被写入到已有内容之后。如果该文件不存在,创建新文件进行写入。
# a+ 打开一个文件用于读写。如果该文件已存在,文件指针将会放在文件的结尾。文件打开时会是追加模式。如果该文件不存在,创建新文件用于读写。
# ab+ 以二进制格式打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。如果该文件不存在,创建新文件用于读写。
# File对象的属性
# 一个文件被打开后,你有一个file对象,你可以得到有关该文件的各种信息。
#
# 以下是和file对象相关的所有属性的列表:
#
# 属性 描述
# file.closed 返回true如果文件已被关闭,否则返回false。
# file.mode 返回被打开文件的访问模式。
# file.name 返回文件的名称。
# file.softspace 如果用print输出后,必须跟一个空格符,则返回false。否则返回true。
fo
=
open
(
"E:/tmp/readme.txt"
,
"a+"
)
#fo.close()
print
"文件名"
,
fo
.
name
print
"是否已经关闭"
,
fo
.
closed
print
"访问模式"
,
fo
.
mode
print
"末尾是否强制加空格"
,
fo
.
softspace
#close() 方法
#file对象的close()方法数显缓冲区里 任何还没写入的信息。 ,并关闭该文件。 这之后便不能再进行写入
#当一个文件对象的引用重新指定给另一个文件时,python会关闭之前的文件。用close()方法关闭文件是一个很好的习惯
#write()方法 。 可以将任何字符串写入一个打开的文件,需要重点注意的是,python字符串可以是二进制数据,而不仅仅是文字。
#write方法 不会在字符串的结尾添加换行('\n')
# fo.write("测试数据")
# fo.close()
# str=fo.read(10)
# print "读取的内容是:" ,str
#文件定位
# 文件定位
# tell()方法告诉你文件内的当前位置, 换句话说,下一次的读写会发生在文件开头这么多字节之后。
#
# seek(offset [,from])方法改变当前文件的位置。Offset变量表示要移动的字节数。From变量指定开始移动字节的参考位置。
#
# 如果from被设为0,这意味着将文件的开头作为移动字节的参考位置。如果设为1,则使用当前的位置作为参考位置。如果它被设为2,那么该文件的末尾将作为参考位置。
#
#
#fo.seek();
# 重命名和删除文件
# # Python的os模块提供了帮你执行文件处理操作的方法,比如重命名和删除文件。
# #
# # 要使用这个模块,你必须先导入它,然后才可以调用相关的各种功能。
# #
# # rename()方法:
# #
# # rename()方法需要两个参数,当前的文件名和新文件名。
# import os
#
# # 重命名文件test1.txt到test2.txt。
# os.rename( "test1.txt", "test2.txt" )
# remove()方法
# 你可以用remove()方法删除文件,需要提供要删除的文件名作为参数。
# mkdir()方法
# 可以使用os模块的mkdir()方法在当前目录下创建新的目录们。你需要提供一个包含了要创建的目录名称的参数。
#
# 语法:
#
# os.mkdir("newdir")
# chdir()方法
# 可以用chdir()方法来改变当前的目录。chdir()方法需要的一个参数是你想设成当前目录的目录名称。
#
# 语法:
#
# os.chdir("newdir")
# import os
#
# # 将当前目录改为"/home/newdir"
# os.chdir("/home/newdir")
# getcwd()方法:
#
# getcwd()方法显示当前的工作目录。
#
# 语法:
#
# os.getcwd()
# rmdir()方法删除目录,目录名称以参数传递。
#
# 在删除这个目录之前,它的所有内容应该先被清除。
#
# 语法:
#
# os.rmdir('dirname')
# 例子:
#
# 以下是删除" /tmp/test"目录的例子。目录的完全合规的名称必须被给出,否则会在当前目录下搜索该目录。
#
# #!/usr/bin/python
# # -*- coding: UTF-8 -*-
#
# import os
#
# # 删除”/tmp/test”目录
# os.rmdir( "/tmp/test" )
You can’t perform that action at this time.