python - 备份和python脚本损坏的问题
问题描述
首先要明确的是,我不精通编程或 python,但通常可以通过一些研究来完成我需要做的事情。请原谅任何不良的格式结构,因为这是我第一次在这样的板上发帖
我最近将我的笔记本电脑从 Ubuntu 18.04 更新到了 20.04。我用 Dejadup 创建了一个完整的系统备份,由于缺少文件,无法恢复。研究让我从 2019 年开始在这里发帖,以手动恢复这些文件。该过程需要 2 个脚本,1 个用于解压缩,第二个用于重建文件,两者均由 Hamish Downer 创建。
首先,
"for f in duplicity-full.*.difftar.gz; do echo "$f"; tar xf "$f"; done"
似乎运行良好,并且确实解压缩了文件。
第二,
#!/usr/bin/env python3
import argparse
from pathlib import Path
import shutil
import sys"
是重构脚本的开始。在我试图重建的目录中使用终端输入第一行并返回。当我输入第二行代码时,终端只是“挂起”而没有任何活动,并且只有在我双击光标时才会返回提示。我没有收到任何错误或警告。当我输入第三行代码时
"from pathlib import Path"
然后返回我得到一个错误
from: can't read /var/mail/pathlib
问题似乎源于“import argparse”命令,我认为是由符号链接引起的。
argparse 位于 /usr/local/lib/python3.8/dist-packages (1.4.0) python3 位于 /usr/bin/
Python 随 Ubuntu 20.04 分发包一起提供。
对于重建这些文件的任何帮助将不胜感激,尤其是在批处理中,因为此脚本旨在执行而不是尝试一次执行一个文件。
更新:我尝试添加此脚本的“重新构造器”部分但没有成功。这是我要使用的脚本的链接:
重构脚本:
class FileReconstructor():
def __init__(self, unpacked_dir, restore_dir):
self.unpacked_path = Path(unpacked_dir).resolve()
self.restore_path = Path(restore_dir).resolve()
def reconstruct_files(self):
for leaf_dir in self.walk_unpacked_leaf_dirs():
target_path = self.target_path(leaf_dir)
target_path.parent.mkdir(parents=True, exist_ok=True)
with target_path.open('wb') as target_file:
self.copy_file_parts_to(target_file, leaf_dir)
def copy_file_parts_to(self, target_file, leaf_dir):
file_parts = sorted(leaf_dir.iterdir(), key=lambda x: int(x.name))
for file_part in file_parts:
with file_part.open('rb') as source_file:
shutil.copyfileobj(source_file, target_file)
def walk_unpacked_leaf_dirs(self):
"""
based on the assumption that all leaf files are named as numbers
"""
seen_dirs = set()
for path in self.unpacked_path.rglob('*'):
if path.is_file():
if path.parent not in seen_dirs:
seen_dirs.add(path.parent)
yield path.parent
def target_path(self, leaf_dir_path):
return self.restore_path / leaf_dir_path.relative_to(self.unpacked_path)
def parse_args(argv):
parser = argparse.ArgumentParser()
parser.add_argument(
'unpacked_dir',
help='The directory with the unpacked tar files',
)
parser.add_argument(
'restore_dir',
help='The directory to restore files into',
)
return parser.parse_args(argv)
def main(argv):
args = parse_args(argv)
reconstuctor = FileReconstructor(args.media/jerry/ubuntu, args.media/jerry/Restored)
return reconstuctor.reconstruct_files()
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))
解决方案
我认为您正在向 shell 输入命令而不是 python 解释器。请检查您的提示,python(以 python3 开头)有>>>
.
Linux 有一个import
命令(ImageMagick 的一部分)并且可以理解import argparse
,但它做了一些完全不同的事情。
import - 将任何可见窗口保存在 X 服务器上并将其输出为图像文件。您可以捕获单个窗口、整个屏幕或屏幕的任何矩形部分。
这符合所描述的行为。import
等待鼠标点击,然后创建一个大的输出文件。检查是否有一个名为argparse
.
可执行脚本包含要由解释器处理的指令,并且有许多可能的解释器、几种 shell(bash 和替代方案)、Perl、Python 等语言,还有一些非常专业nft
的防火墙规则。
如果从命令行执行脚本,shell 会读取它的第一行。如果它以#!
字符开头(称为“shebang”),则使用该行中列出的程序。(注意:/usr/bin/env 只有一个帮助程序来查找程序的确切位置)。
但是如果你想以交互方式使用解释器,你需要显式地启动它。在这种情况下,shebang 行没有特殊含义,仅作为脚本的第一行。否则,它只是一个注释并被忽略。
推荐阅读
- javascript - 在地图上异步显示大量数据 [传单]
- flask - 从包含路径参数的路径提供 Dash
- python - 在pyparsing中根据语法生成字符串
- spring-boot - 这里有什么区别 - @Autowired 和 @MockBean
- c# - 为什么在使用 HttpContext 时会出现错误?
- excel - 创建新的动态文件夹并将一个主要的 excel 文件拆分到其中
- snowflake-cloud-data-platform - 雪花合并使用同一张表
- python - 在 python 中组合 RSS 提要
- python - scipy:如何在约束条件下最小化最小残差平方和?
- go - 为什么 kill -15 没有优雅地杀死我的 Golang gRPC 服务?