问题描述
该笔记将记录:在 Python 中,常用文件操作,以及常见问题处理。
解决方案
WIP
输入输出
username = input("Username:")
password = input("Password:")
文件操作
读取文件
# open and read the file after the appending:
f = open("demofile2.txt", "r")
file_content = f.read()
f.close()
# with open()
with open("demofile2.txt", "r") as f:
file_content = f.read()
print(file_content)
写入文件
f = open("demofile2.txt", "a")
f.write("Now the file has more content!")
f.close()
常用文件操作
################################################################################################################################################################
# Read only the first line of a file?
# https://stackoverflow.com/questions/1904394/read-only-the-first-line-of-a-file
################################################################################################################################################################
infile = open('filename.txt', 'r')
firstLine = infile.readline()
infile.readline() # String
infile.readlines() # List
infile.writelines([""]) #
################################################################################################################################################################
# Prepend line to beginning of a file
# https://stackoverflow.com/questions/5914627/prepend-line-to-beginning-of-a-file
################################################################################################################################################################
def line_prepender(filename, line):
with open(filename, 'r+') as f:
content = f.read()
f.seek(0, 0)
f.write(line.rstrip('\r\n') + '\n' + content)
################################################################################################################################################################
# Python Recipe: Open a file, read it, print matching lines
# https://palewi.re/posts/2008/04/05/python-recipe-open-a-file-read-through-it-print-each-line-matching-a-search-term/
################################################################################################################################################################
for line in open("wssnt10.txt", "r"):
if re.match("(.*)(L|l)ove(.*)", line):
print line,
################################################################################################################################################################
# How do I specify new lines on Python, when writing on files?
# https://stackoverflow.com/questions/11497376/how-do-i-specify-new-lines-on-python-when-writing-on-files
# 换行符可以使用「os.linesep」,但是如果是写入文件的话,直接使用「\n」即可,因为Python会自动处理。
################################################################################################################################################################
################################################################################################################################################################
# Directing print output to a .txt file in Python 3
# https://stackoverflow.com/questions/36571560/directing-print-output-to-a-txt-file-in-python-3
################################################################################################################################################################
print("I have a question.", file=open("output.txt", "a"))
################################################################################################################################################################
# 获取文件的修改时间
################################################################################################################################################################
import os
mtime = os.path.getmtime(file_path)
################################################################################################################################################################
# Python os.utime() Method
# https://www.tutorialspoint.com/python/os_utime.htm
################################################################################################################################################################
# Modifying atime and mtime
os.utime("a2.py",(1330712280, 1330712292))
获取代码文件路径
# 获取类文件路径 import inspect inspect.getfile(C.__class__) # 获取 .py 文件路径 import os file_paht = os.path.abspath(__file__) file_path = os.path.realpath(__file__) folder = os.path.dirname(file_path)
参考文献
w3school/Python File Write
How do I get the filepath for a class in Python? – Stack Overflow
python – Get location of the .py source file – Stack Overflow
How do I get the path of the Python script I am running in? – Stack Overflow