[Python] handles multi-level directory files under Windows and uploads to Linux servers

Source: Internet
Author: User
Tags create directory

#-*-coding:utf-8-*-__author__='TSBC'Importsysreload (SYS) sys.setdefaultencoding ('Utf-8')ImportParamikoImportOSImport TimeclassUpload ():"""upload a daily generated test report to a Linux Web server. Paramiko package needs to be installed/result/the HTML file/result/image file is stored"""username="Root"Password="******"hostname="192.168.200.8"Localdir=".. \\result\\"Romotedir="/opt/lampp/htdocs/result/"Port= 22#Date format 2014-12-17Day = Time.strftime ('%y-%m-%d', Time.localtime (Time.time ()))#gets the current time format 2014-12-12-10_39_58now = Time.strftime ('%y-%m-%d-%h_%m_%s', Time.localtime (Time.time ()))#define Shell Create Directory command: Determine if the path exists,Mkdir_day ="[-D \ ""+ Romotedir + day +"\" ] || mkdir"+ Romotedir + day +"; "mkdir_img="[-D \ ""+ Romotedir + day +"/image\ "] | | mkdir"+ Romotedir + day +"/image"    #Print Mkdir_day    #Print mkdir_img    """ssh telnet to the Linux host"""    #Call the SSH client interface to log ins =Paramiko. Sshclient ()#S.load_system_host_keys ()S.set_missing_host_key_policy (Paramiko. Autoaddpolicy ()) S.connect (hostname=hostname, Username=username, password=password)#use the Linux standard input, output, error, call the defined shell command to create the directoryStdin,stdout,stderr=s.exec_command (Mkdir_day +mkdir_img)#Output Command Feedback    PrintStdout.read ()#Close Connections.close ()"""the Os.walk () output is: ([path],[directory],[file]) ('.. \\result\\2014-12-18 ', [' image '], [' 2014-12-18-10_42_43_result.html ']) (' ... \\result\\2014-12-18\\image ', [], [' 2014-12-18-11_28_33_login.png ', ' 2014-12-18-11_28_36_login.png '])"""Path= Localdir + Dayifos.path.exists (path): Nested=os.walk (path) forIinchNested:PrintI TT= I[1]#I[0] is a path; i[1] Directory Name list; i[2] is a list of files            ifLen (TT) > 0:#When tt>0, the description is the root directory (the entry directory), tt<0 when the description is currently a leaf directory                PrintU"= + current directory"+ str (i[0]) + u"The following files are:"FF= I[0].split ('\\')#using \ for path splittingDdir = Ff[len (FF)-1]#gets the file directory name at the end                 forJinchI[2]: Localn= Localdir + Ddir +"\\"+J Romoten= Romotedir + Ddir +"/"+JPrintJ#Print Localn                    #Print Romoten                    Try:                        #Connect to the remote host by host name, port, user name, and password. t =Paramiko. Transport (hostname, port) t.connect (username=username, password=password) sftp=Paramiko. Sftpclient.from_transport (t)#an sftp put means that a local file is transferred to a remote machine, and a get indicates that the remote file is passed to the local machine. sftp.put (Localn, Romoten)#Close Connectiont.close ()exceptException, E:Importtraceback traceback.print_exc ()Try: T.close ()except:                            Pass            elifLen (TT) < 1:                PrintU"= + current directory"+ str (i[0]) + u"The following files are:"FF= I[0].split ('\\')#using \ for path splittingFdir = Ff[len (FF)-1]#gets the file directory name at the end                 forJinchI[2]: Localn= str (i[0]) +"\\"+ j#str (i[0]) = = Localdir + FdirRomoten ="/opt/lampp/htdocs/result/"+ Day +"/"+ Fdir +"/"+JPrintJ#Print Localn                    #Print Romoten                    #Stdin,stdout,stderr=s.exec_command ("mkdir/opt/lampp/htdocs/result/" +fdir)                    Try: T=Paramiko. Transport (hostname, port) t.connect (username=username, password=password) sftp=Paramiko. Sftpclient.from_transport (t)#an sftp put means that a local file is transferred to a remote machine, and a get indicates that the remote file is passed to the local machine. sftp.put (Localn, Romoten) t.close ()exceptException, E:Importtraceback traceback.print_exc ()Try: T.close ()except:                            Pass    Else:        Printpath+"The directory does not exist and the report failed to upload successfully! "if __name__=="__main__": Upload ()

[Python] handles multi-level directory files under Windows and uploads to Linux servers

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.