创建一个确定操作系统的简单程序,然后在单独的文件夹中创建目录。然后在计算机上获取桌面和文档并将其保存到新创建的目录
中问题是:它似乎没有将实际桌面上的内容实际保存到新创建的Desktop文件夹中。但它确实创建了新文件夹
import os
import shutil
import platform
login = os.getlogin()
# Creates Desktop/Documents directories
if platform.system().lower() == 'darwin':
#do Mac shit
newDesktop = r'/Users/%s/WorkDocs/Desktop' %(login)
if not os.path.exists(newDesktop):
os.makedirs(newDesktop)
newDocuments = r'/Users/%s/WorkDocs/Documents' %(login)
if not os.path.exists(newDocuments):
os.makedirs(newDocuments)
elif platform.system().lower() =='windows':
# do windows shit
newDesktop = r'C:\Users\%s\WorkDocs\Desktop' %(login)
if not os.path.exists(newDesktop):
os.makedirs(newDesktop)
newDocuments = r'C:\Users\%s\WorkDocs\Documents'
if not os.path.exists(newDocuments):
os.makedirs(newDocuments)
else:
print('Only Mac and Windows are supported')
# Saves Desktop/Documents into previously created directories
if platform.system().lower() == 'darwin':
os.path.join(r'/Users/%s/Desktop' %(login), r'/Users/%s/WorkDocs/Desktop' %(login))
os.path.join(r'/Users/%s/Documents' %(login), r'/Users/%s/WorkDocs/Documents' %(login))
elif platform.system().lower() =='windows':
os.path.join(r'C:\Users\%s\Documents' %(login), r'C:\Users\%s\WorkDocs\Documents' %(login))
os.path.join(r'C:\Users\%s\Desktop' %(login), r'C:\Users\%s\WorkDocs\Desktop' %(login))
else:
print('OS could not be determined')
答案 0 :(得分:2)
您可以使用shutil模块和 from os.path import expanduser, join
import shutil
home = expanduser('~')
src_desk = join(home, 'Desktop')
src_docs = join(home, 'Documents')
dest_desk = join(home, 'WorkDocs', 'Desktop')
dest_docs = join(home, 'WorkDocs', 'Documents')
shutil.rmtree(dest_desk)
shutil.rmtree(dest_docs)
shutil.copytree(src_desk, dest_desk)
shutil.copytree(src_docs, dest_docs)
{{1}}