Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Korakot Chaovavanich http://korakot.stumbleupon.com

« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS 

Print to a file

To print to console, it's easy
   1  print 'hello', 'world'

To print to an output file, it's similar
   1  f = open('out.txt','w')
   2  print >>f, 'hello', 'world'

Transfer files on windows network

From Fadly Tabrani's recipe.
You need his implementation of netcopy, netmove, netdelete.
pywin32 extension required.
   1  
   2  # Copy "c:\documents" folder/file to "c:\transferred" on host "w0001".
   3  netcopy('w0001', 'c:\\documents', 'c:\\transferred')
   4  
   5  # Move with account credentials.
   6  netmove('w0001', 'c:\\documents', 'c:\\transferred', 'admin', 'adminpass')
   7  
   8  # Delete with another account.
   9  netdelete('w0001', 'c:\\transferred', 'testdom\\user1', 'user1pass')

Reading bytes value

   1  
   2  from struct import unpack
   3  f = open('data.bin', 'rb')
   4  
   5  b = f.read(1)
   6  b2 = f.read(2)
   7  b4 = f.read(4)
   8  
   9  n = ord(b)  # from char to int
  10  n4 = unpack('>L', b4)  # big endian
  11  n4 = unpack('<L', b4)  # little endian
  12  n = unpack('>h', b2)   # big endian short

Upload file with http client (multipart/form-data)

   1  
   2  def post_multipart(host, selector, fields, files):
   3      """
   4      Post fields and files to an http host as multipart/form-data.
   5      fields is a sequence of (name, value) elements for regular form fields.
   6      files is a sequence of (name, filename, value) elements for data to be uploaded as files
   7      Return the server's response page.
   8      """

See the rest of the implementation here

Send a file using FTP

   1  
   2  import ftplib
   3  s = ftplib.FTP('myserver.com','login','password') # Connect
   4  
   5  f = open('todo.txt','rb')                # file to send
   6  s.storbinary('STOR todo.txt', f)         # Send the file
   7  
   8  f.close()                                # Close file and FTP
   9  s.quit()

Taken (with some mod.) from here
« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS