简体   繁体   English

我可以从以前运行的 python 脚本中读取代码吗

[英]Can I read code from a previously run python script

I ran a python file using the command python file.py and it executed successfully.我使用命令 python file.py 运行了一个 python 文件并成功执行。 Right after I managed to delete the file, can I recover the code that was run in the previous command?在我设法删除文件后,我可以恢复在上一个命令中运行的代码吗? I still have the terminal open and have not typed anything else into it.我仍然打开终端并且没有在其中输入任何其他内容。 Running ubuntu.运行 ubuntu。

if you had imported the file instead of running it directly, a .pyc file would have been created containing the compiled bytecode which you could easily transform back into the regular python code (minus the comments.) However, you did run the script directly, so there is no .pyc file.如果您导入了文件而不是直接运行它,则会创建一个包含已编译字节码的 .pyc 文件,您可以轻松地将其转换回常规的 Python 代码(减去注释)。但是,您确实直接运行了脚本,所以没有 .pyc 文件。

If you deleted the file in a GUI file browser, the file might have been sent into a "trash bin" where you might be able to recover it from.如果您在 GUI 文件浏览器中删除了该文件,则该文件可能已被发送到“垃圾箱”中,您可以从中恢复它。

Assuming you deleted the file using the "rm" command in the terminal, however, the data might still be on the disk provided it hasn't been overwritten ("deleting" files normally just marks the data to be overwritten on the disk)假设您在终端中使用“rm”命令删除了文件,但是,如果数据没有被覆盖,则数据可能仍在磁盘上(“删除”文件通常只是在磁盘上标记要覆盖的数据)

If you're lucky, you might be able to recover it.如果幸运的话,您可能能够恢复它。 The process isn't exactly simple, though, and there's no guarantee that the file hasn't already been overwritten since the disk is pretty much in constant use when you're using the system.然而,这个过程并不完全简单,并且不能保证文件没有被覆盖,因为在您使用系统时磁盘几乎一直在使用。 More info on that here:更多信息在这里:

https://help.ubuntu.com/community/DataRecovery https://help.ubuntu.com/community/DataRecovery

There's also a handy utility called lsof which you can use to recover a 'deleted' file if there's still an open file handle for it:还有一个名为 lsof 的方便实用程序,如果仍有打开的文件句柄,您可以使用它来恢复“已删除”的文件:

https://support.hpe.com/hpsc/doc/public/display?docId=emr_na-c00833030 https://support.hpe.com/hpsc/doc/public/display?docId=emr_na-c00833030

Also, in the future, I recommend using "rm -i" instead of just rm with no options as that will at least prompt you to confirm if you're sure you want to delete something first.此外,将来,我建议使用“rm -i”而不是仅使用没有选项的 rm ,因为这至少会提示您确认是否确定要先删除某些内容。 You can also make an alias for this in your shell so that regular rm just points to 'rm -i'您还可以在 shell 中为此创建别名,以便常规 rm 仅指向 'rm -i'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM