【发布时间】:2017-06-27 18:16:40
【问题描述】:
我对 python 比较陌生,我正在做一些项目。假设我在 Windows 的 D 分区上运行脚本,例如,它是“D:/quarantine.py”
我现在正在寻找的是:
- 从一个文件夹(例如桌面)获取文件并将其移动到另一个文件夹(例如 C:\Quarantine) - 但我需要从键盘读取文件和目录。 C:\Quarantine 文件夹是之前使用以下代码创建的:
def create_quar(quar_folder): try: os.makedirs(quar_folder) except OSError as exception: if exception.errno != errno.EEXIST: raise dir_name = input("Enter the desired quarantine path: ") if os.path.isdir(dir_name): print ("Directory already existed.") else: print ("Directory created successfully!") create_quar(dir_name)
在移动文件之前,我需要以某种方式存储文件的先前位置。我正在考虑在 C:\Quarantine 文件夹中创建一个 .txt 文件。
如果我改变主意,我会调用一个函数来读取我之前创建的 .txt 文件,然后将文件移回原始文件夹。这个,我不知道怎么实现。
我不知道该怎么做。正在考虑这样的事情来记录路径和移动文件:
path = input("Directory of file I need to move: ")
file = input("File name: ")
f = open(dir_name+"\log.txt",'w')
f.write(os.path.abspath(path+file))
shutil.move(path+file,dir_name+file)
dir_name 是我之前用来读取隔离文件夹位置的变量,所以我想我可以重复使用它。至于读取日志文件和恢复,我不知道。
谁能帮忙?
【问题讨论】:
-
去看看这个链接:它有很多信息可以帮助你。 stackoverflow.com/questions/11574257/…另请看:docs.python.org/library/logging.html
-
老实说,我认为我需要比日志模块更基本的东西。该文本文件一次不需要保存超过 1 个路径