我对python比较陌生,我正在做一些项目。假设我在Windows上的分区D上运行脚本,例如,它是“D:/检疫.py““
我现在要找的是:
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:\quantilation文件夹中创建一个.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是我之前用来读取隔离文件夹位置的变量,所以我想我可以重用它。至于读取日志文件和恢复,我不知道。在
有人能帮忙吗?在
好吧,最后我还是自己完成了。如果有人感兴趣,您可以找到下面的代码示例。它非常简单,当然可以优化,但它确实有效。在
主要:
移动:
^{pr2}$还原:
你可以用操作系统()从操作系统导入。它将在cmd/shell中执行命令,但仅针对子进程。 我希望这是有益的
相关问题 更多 >
编程相关推荐