首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用python的FileTransfer

使用python的FileTransfer
EN

Stack Overflow用户
提问于 2017-07-31 01:08:56
回答 1查看 81关注 0票数 0

编写一个简单的程序来确定操作系统,然后在单独的文件夹中创建目录。然后获取计算机上的桌面和文档并将其保存到新创建的目录中

问题是:它似乎并没有真正将实际桌面上的内容保存到新创建的桌面文件夹中。但它确实会创建新文件夹

代码语言:javascript
复制
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')
EN

回答 1

Stack Overflow用户

发布于 2017-07-31 01:23:52

您可以使用shutil模块和os.path.expanduser('~')

代码语言:javascript
复制
    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)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45402501

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档