How I Open Remote Server Folder Using Python

How to access a file hosted on a public remote server (python)?

urllib.request.urlopen is like open, but for URLs:

from urllib.request import urlopen
with urlopen("http://afakesite.org/myfile.tsv") as f:
# read f like a file object

Note that the object produces bytes, i.e. behaves like a file opened in binary mode. Thus, you can't read it line by line, rather in chunks of certain size.

If the file is not too big, you can read it all at once:

lines = f.read().decode('utf-8').split('\n')

comapare files that are on remote directory and local directory using python

def getFilesList(path):
files = []
for (dirpath, dirnames, filenames) in os.walk(path):
files.extend(filenames)
return files
ServerFiles = getFilesList(Srverpath)
LocalFiles = getFilesList(Lclpath)
fileDiffList = []
for file in ServerFiles:
if file in LocalFiles:
pass
else:
fileDiffList.append(file)

We can get the uncommon files by using 2 separate lists.
Call getFilesList method twice by passing your server path and local file path.
At the end your 'fileDiffList' will have file names

access remote files on server with smb protocol python3

A simple example of opening a file using urllib and pysmb in Python 3

import urllib
from smb.SMBHandler import SMBHandler
opener = urllib.request.build_opener(SMBHandler)
fh = opener.open('smb://host/share/file.txt')
data = fh.read()
fh.close()

I haven't got an anonymous SMB share ready to test it with, but this code should work.

urllib2 is the python 2 package, in python 3 it was renamed to just urllib and some stuff got moved around.



Related Topics



Leave a reply



Submit