当前位置:网站首页>Unfortunately, I broke the leader's confidential documents and spit blood to share the code skills of backup files
Unfortunately, I broke the leader's confidential documents and spit blood to share the code skills of backup files
2022-04-23 09:15:00 【Python Programming Jacko】
What I share today is not specific Python Knowledge point , But a very useful practical experience .
Throw questions
Let's assume such a scenario :
- You have to write a Python Program , Every day in the morning 8 Generate or update an important document , Call 「maishu.txt」 Well .
- Suppose the file size is 100M, You may need to generate and write files 1 About minutes .
- This document is placed in a directory , Every day 24 People or other programs access and read... Every hour .
What can be done to ensure the following highlights :
1. Ensure that this file can be used at all times , Or maximize available time .
2. If something goes wrong , You can recover immediately , Will not affect the user .
Before continuing reading , Think about it first :「 What would you do ?」
Stupid way
First, let's talk about the practice of ordinary young people , And the worst thing to do .
Here is the pseudo code , Can not run , It is only used to explain the idea of solving the problem .
with open('maishu.txt', 'w') as f:
# Code logic for writing files
# The write time is about one minute
The problem with this approach is :
- Once you start writing , The file will be emptied and gradually added , This will cause the person reading at that time to read an empty file , Or incomplete documents , Cause the reader to crash
- in other words , About 1 Minute time , This file is not available , It may cause serious errors downstream .
- If there is an error in the writing process , Can't recover instantly , It will cause system interruption , Manual intervention may be required
The major problems brought about by this approach , I've seen it several times in practical work . The file read is illegal , Read or write program crash and so on . Colleagues who make mistakes , I don't know where I am anymore .
A good way
A better way is this :
- Write another file first , This does not affect the reading and use of the original file
- After writing , Then rename the file .
The pseudocode is as follows :
with open('maishu20220425.txt', 'w') as f:
# Code logic for writing files
# The write time is about one minute
# Back up the original file
rename('maishu.txt', 'maishu20220424.txt')
# Rename the new file to the main file
rename('maishu20220425.txt', 'maishu.txt')
Because the last moment is just renaming the file , It's a quick process , It's not easy to make a mistake . Even if something goes wrong , You can also restore the original file . Therefore, the above problems can be well solved .
A little more improvement
Because the above backup will produce multiple files , Take up a lot of space , So the general code will only keep 5 The latest data . The pseudocode is as follows :
with open('maishu20220425.txt', 'w') as f:
# Code logic for writing files
# The write time is about one minute
# Back up the original file
rename('maishu.txt', 'maishu20220424.txt')
# Rename the new file to the main file
rename('maishu20220425.txt', 'maishu.txt')
# Delete the oldest backup , Keep only the latest 5 Share
delete_backup(keep=5)
This approach is basically an industry best practice , This is done on many occasions . For example, automated deployment usually does this . In this way, the normal operation of the program will not be affected during the release process , Once something goes wrong, you can immediately roll back to the previous version .
See? ? Next time, if you have a similar occasion , Remember to use the above method , Lest I want to show my code skills , It turned out to be self defeating
About Python Technology reserve
Learn from good examples Python Whether it's employment or sideline, it's good to make money , But learn to Python Still have a learning plan . Finally, let's share a complete set of Python Learning materials , For those who want to learn Python Let's have a little help !
One 、Python Learning routes in all directions
Python All directions are Python Sort out the common technical points , Form a summary of knowledge points in various fields , The use of it is , You can find the corresponding learning resources according to the above knowledge points , Make sure you learn more comprehensively .

Two 、 Learning software
If a worker wants to do a good job, he must sharpen his tools first . Study Python Common development software is here , It saves you a lot of time .

3、 ... and 、 Getting started video
When we were watching videos to learn , You can't just move your eyes and brain without hands , A more scientific way to learn is to use them after understanding , At this time, the hand training program is very suitable .

Four 、 Practical cases
Optical theory is useless , Learn to knock together , Do it , Can you apply what you have learned to practice , At this time, we can make some practical cases to learn .

5、 ... and 、 Interview information
We learn Python Must be to find a well paid job , The following interview questions are from Ali 、 tencent 、 The latest interview materials of big Internet companies such as byte , And the leader Ali gave an authoritative answer , After brushing this set of interview materials, I believe everyone can find a satisfactory job .


This full version of Python A full set of learning materials has been uploaded CSDN, Friends can scan the bottom of wechat if necessary CSDN The official two-dimensional code is free 【 Guarantee 100% free 】

Python Information 、 technology 、 Course 、 answer 、 For consultation, you can also directly click on the business card below ,
Add official customer service Qi↓
版权声明
本文为[Python Programming Jacko]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230637464351.html
边栏推荐
- npm ERR! network
- 2022-04-22 openebs cloud native storage
- L2-024 tribe (25 points) (and check the collection)
- Go language self-study series | golang method
- Brief steps to build a website / application using flash and H5
- 小女孩行走
- Introduction to matlab
- Go language self-study series | golang structure as function parameter
- Four pictures to understand some basic usage of Matplotlib
- Thread scheduling (priority)
猜你喜欢

Notes on xctf questions

LeetCode_DFS_中等_1254. 统计封闭岛屿的数目

BK3633 规格书

Introduction to matlab

112. 路径总和

Share the office and improve the settled experience

Applet error: cannot read property'currenttarget'of undefined

PLC point table (register address and point table definition) cracking detection scheme -- convenient for industrial Internet data acquisition

Correct method of calculating inference time of neural network

Failed to download esp32 program, prompting timeout
随机推荐
Valgrind et kcachegrind utilisent l'analyse d'exécution
【读书笔记】《Verilog数字系统设计教程》 第5章 条件语句、循环语句和块语句(附思考题答案)
Get trustedinstaller permission
How to read excel table to database
Kettle实验 (三)
npm ERR! network
Machine learning (VI) -- Bayesian classifier
Correct method of calculating inference time of neural network
【SQL server速成之路】数据库的视图和游标
On array replication
SQL used query statements
About CIN, scanf and getline, getchar, CIN Mixed use of getline
《数字电子技术基础》3.1 门电路概述、3.2 半导体二极管门电路
Concave hull acquisition method based on convex hull of point cloud
Open services in the bottom bar of idea
npm ERR! network
[58] length of the last word [leetcode]
First principle mind map
Number of islands
Initial experience of talent plan learning camp: communication + adhering to the only way to learn open source collaborative courses