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
How to Reliably Open a File in the Same Directory as the Currently Running Script
What Is Different Between Makedirs and Mkdir of Os
The Correct Cmakelists.Txt File to Call a Maxon Libarary in a Python Script Using Pybind11
List Comprehension with If Statement
How to Make an Immutable Object in Python
Python Script to Do Something at the Same Time Every Day
Explaining the 'Self' Variable to a Beginner
Creating a New Corpus with Nltk
":=" Syntax and Assignment Expressions: What and Why
How to Specify New Lines on Python, When Writing on Files
Detect Socket Hangup Without Sending or Receiving
Basic Python Hello World Program Syntax Error
Sharing a Result Queue Among Several Processes
Pandas: Drop a Level from a Multi-Level Column Index
How to Remove Non-Ascii Characters But Leave Periods and Spaces