簡體   English   中英

使用Python的FileTransfer

[英]FileTransfer with python

制作一個簡單的程序來確定操作系統,然后在單獨的文件夾中創建目錄。 然后在計算機上獲取桌面和文檔,並將其保存到新創建的目錄中

問題是:似乎沒有將實際桌面上的內容實際保存到新創建的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')

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

    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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM