I want a program which creates a backup of all my important files by Python

本文介绍了一个使用Python编写的备份脚本,该脚本能够指定要备份的文件和目录,并将其压缩成zip文件存储到指定的备份目录中。脚本支持用户输入备注信息作为zip文件的一部分名称,并通过命令行输出备份状态。
#!/usr/bin/python
# Filename: backup_ver4.py
import os
import time
# 1. The files and directories to be backed up are specified in a list.
source = ['"C:\\My Documents"', 'C:\\Code']
# Notice we had to use double quotes inside the string for names with spaces in it.
# 2. The backup must be stored in a main backup directory
target_dir = 'E:\\Backup' # Remember to change this to what you will be using
# 3. The files are backed up into a zip file.
# 4. The current day is the name of the subdirectory in the main directory
today = target_dir + os.sep + time.strftime('%Y%m%d')
# The current time is the name of the zip archive
now = time.strftime('%H%M%S')
# Take a comment from the user to create the name of the zip file
comment = input('Enter a comment --> ')
if len(comment) == 0: # check if a comment was entered
target = today + os.sep + now + '.zip'
else:
target = today + os.sep + now + '_' + \
comment.replace(' ', '_') + '.zip'
# Create the subdirectory if it isn't already there
if not os.path.exists(today):
os.mkdir(today) # make directory
print('Successfully created directory', today)
# 5. We use the zip command to put the files in a zip archive
zip_command = "zip -qr {0} {1}".format(target, ' '.join(source))
# Run the backup
if os.system(zip_command) == 0:
print('Successful backup to', target)
else:
print('Backup FAILED')

Output:
$ python backup_ver4.py
Enter a comment --> added new examples
Successful backup to
E:\Backup\20080702\202836_added_new_examples.zip
$ python backup_ver4.py
Enter a comment -->
Successful backup to E:\Backup\20080702\202839.zip
Here's an example Python program to make an Nth order polynomial fit without using numpy: ```python def polynomial_fit(x, y, degree): # Create a Vandermonde matrix of the given degree vandermonde = [[xi**i for i in range(degree+1)] for xi in x] # Solve the linear system to obtain the coefficients of the polynomial coefficients = gaussian_elimination(vandermonde, y) # Create a lambda function to represent the polynomial polynomial = lambda xi: sum([coefficients[i]*xi**i for i in range(degree+1)]) return polynomial, coefficients def gaussian_elimination(A, b): # Perform Gaussian elimination with partial pivoting to solve Ax=b n = len(A) for i in range(n): # Find the pivot row and swap with current row pivot_row = max(range(i, n), key=lambda j: abs(A[j][i])) A[i], A[pivot_row] = A[pivot_row], A[i] b[i], b[pivot_row] = b[pivot_row], b[i] # Eliminate lower rows for j in range(i+1, n): factor = A[j][i]/A[i][i] for k in range(i+1, n): A[j][k] -= factor*A[i][k] b[j] -= factor*b[i] # Back-substitute to obtain solution x = [0]*n for i in range(n-1, -1, -1): x[i] = (b[i] - sum([A[i][j]*x[j] for j in range(i+1, n)]))/A[i][i] return x # Example usage x = [1, 2, 3, 4, 5] y = [2.1, 3.2, 4.3, 5.4, 6.5] degree = 3 polynomial, coefficients = polynomial_fit(x, y, degree) print("Polynomial coefficients:", coefficients) print(f"Polynomial function: {coefficients[0]:.2f} + {coefficients[1]:.2f}x + {coefficients[2]:.2f}x^2 + {coefficients[3]:.2f}x^3") print("Predicted value at x=6:", polynomial(6)) ``` This program defines two functions: `polynomial_fit` and `gaussian_elimination`. The `polynomial_fit` function takes in three parameters: `x` and `y` are lists representing the input and output data, and `degree` is the degree of the desired polynomial fit. It first creates a Vandermonde matrix of the given degree using the input data, and then solves the linear system `Ax=b` to obtain the coefficients of the polynomial using the `gaussian_elimination` function. Finally, it creates a lambda function representing the polynomial and returns it along with the coefficients. The `gaussian_elimination` function is a simple implementation of Gaussian elimination with partial pivoting to solve a linear system `Ax=b`. It first performs partial pivoting to find the pivot element of each row, and then eliminates the lower rows using the pivot element. Finally, it performs back-substitution to obtain the solution. To use the `polynomial_fit` function, simply call it with the input and output data and the desired degree of the polynomial fit. It returns a lambda function representing the polynomial and a list of coefficients. You can then use the lambda function to predict output values at new input values.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值