Unable to download file with pysftp get - python

I am able to connect the sftp with pysftp successfully, but getting error while downloading file as FileNotFoundError: [Errno 2] No such file. I also observed that the file is just creating at local path adding '?' along with extension. Below are more details.
File at ftp as Test_03132018080105.csv.
File creating at Local path as Test_03132018080105.csv? with zero bytes
Code:
def get_move_on_ftp(ftpsource,localsource):
if (os.stat(envvar.validfiles).st_size == 0) and (os.stat(envvar.invalidfiles).st_size == 0):
print("There are no Source files on FTP.")
else:
srcinfo={'host':envvar.src_ftphost,'port':envvar.src_ftpport,'username':envvar.src_uname,'password':envvar.src_passwd}
sftp = pysftp.Connection(**srcinfo)
sftp.cwd(ftpsource)
''' Downloading Files '''
avail_files=open(envvar.validfiles,'r')
for filename in avail_files:
print(sftp.getcwd())
#sftp.get(filename, preserve_mtime=True)
print(filename) # for debug
sftp.get(filename)
sftp.close()
Error:
Traceback (most recent call last):
File "my.py", line 96, in <module>
main()
File "my.py", line 92, in main
config_file_read(config_file)
File "my.py", line 85, in config_file_read
get_move_on_ftp(ftpsource,localsource)
File "my.py", line 61, in get_move_on_ftp
sftp.get(filename)
File "/home/username/miniconda3/lib/python3.6/site-packages/pysftp/__init__.py", line 249, in get
self._sftp.get(remotepath, localpath, callback=callback)
File "/home/username/miniconda3/lib/python3.6/site-packages/paramiko/sftp_client.py", line 770, in get
size = self.getfo(remotepath, fl, callback)
File "/home/username/miniconda3/lib/python3.6/site-packages/paramiko/sftp_client.py", line 746, in getfo
file_size = self.stat(remotepath).st_size
File "/home/username/miniconda3/lib/python3.6/site-packages/paramiko/sftp_client.py", line 460, in stat
t, msg = self._request(CMD_STAT, path)
File "/home/username/miniconda3/lib/python3.6/site-packages/paramiko/sftp_client.py", line 780, in _request
return self._read_response(num)
File "/home/username/miniconda3/lib/python3.6/site-packages/paramiko/sftp_client.py", line 832, in _read_response
self._convert_status(msg)
File "/home/username/miniconda3/lib/python3.6/site-packages/paramiko/sftp_client.py", line 861, in _convert_status
raise IOError(errno.ENOENT, text)
FileNotFoundError: [Errno 2] No such file

Just add "/" in front of ftpsource. i.e. "/2020/Jan/10/". It should work

Related

Download audio only youtube raises regexmatcherror

I am trying to execute the following code, that is supposed to download the audio file from youtube url
from pathlib import Path
from pytube import YouTube
def download_youtube_video(youtube_url, output_path):
audio_file = YouTube(youtube_url).streams.get_audio_only().download(output_path=output_path)
audio_file = Path(audio_file)
audio_file = audio_file.replace(audio_file.with_suffix(".mp3"))
return audio_file
youtube_url = 'https://youtu.be/_H5hsUwv8lE'
output_path = Path(__file__).parent
audio_file = download_youtube_video(youtube_url, output_path)
But I got the following traceback
Traceback (most recent call last):
File "C:\Users\Future\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\__main__.py", line 181, in fmt_streams
extract.apply_signature(stream_manifest, self.vid_info, self.js)
File "C:\Users\Future\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\extract.py", line 409, in apply_signature
cipher = Cipher(js=js)
File "C:\Users\Future\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\cipher.py", line 43, in __init__
self.throttling_plan = get_throttling_plan(js)
File "C:\Users\Future\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\cipher.py", line 405, in get_throttling_plan
raw_code = get_throttling_function_code(js)
File "C:\Users\Future\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\cipher.py", line 311, in get_throttling_function_code
name = re.escape(get_throttling_function_name(js))
File "C:\Users\Future\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\cipher.py", line 296, in get_throttling_function_name
raise RegexMatchError(
pytube.exceptions.RegexMatchError: get_throttling_function_name: could not find match for multiple
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "demo.py", line 18, in <module>
audio_file = download_youtube_video(youtube_url, output_path)
File "demo.py", line 6, in download_youtube_video
audio_file = YouTube(youtube_url).streams.get_audio_only().download(output_path=output_path)
File "C:\Users\Future\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\__main__.py", line 296, in streams
return StreamQuery(self.fmt_streams)
File "C:\Users\Future\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\__main__.py", line 188, in fmt_streams
extract.apply_signature(stream_manifest, self.vid_info, self.js)
File "C:\Users\Future\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\extract.py", line 409, in apply_signature
cipher = Cipher(js=js)
File "C:\Users\Future\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\cipher.py", line 43, in __init__
self.throttling_plan = get_throttling_plan(js)
File "C:\Users\Future\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\cipher.py", line 405, in get_throttling_plan
raw_code = get_throttling_function_code(js)
File "C:\Users\Future\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\cipher.py", line 311, in get_throttling_function_code
name = re.escape(get_throttling_function_name(js))
File "C:\Users\Future\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\cipher.py", line 296, in get_throttling_function_name
raise RegexMatchError(
pytube.exceptions.RegexMatchError: get_throttling_function_name: could not find match for multiple
Any idea how to fix such a problem?
My steps to solve the problem:
First, detect the package pytube path using the code
import pytube
import os
print(pytube.__file__)
print(os.path.dirname(pytube.__file__))
Navigate to the directory pytube and modify the cipher.py
The line 273 r'\([a-z]\s*=\s*([a-zA-Z0-9$]{3})(\[\d+\])?\([a-z]\)', changed to r'\([a-z]\s*=\s*([a-zA-Z0-9$]+)(\[\d+\])?\([a-z]\)',
The line 288 nfunc=function_match.group(1)), changed to nfunc=re.escape(function_match.group(1))),

permission errors while using pysftp and paramiko

I have a use case to transfer file over sftp, planning to use paramiko or pysftp modules of python3.7 and having troubles with one of the destinations/targets. File transfer works fine when winSCP is used. Any suggestions to resolve this issue or what could be the specific reason for this target.
Approach#1: using paramiko and open_sftp
import paramiko
import os
ssh_client =paramiko.SSHClient()
ssh_client.load_system_host_keys()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname='abc.com', username='username', password='pwd')
sftp_client=ssh_client.open_sftp()
print(sftp_client.stat(path='/receive/file))
sftp_client.file("createfile","w+")
print(sftp_client.listdir(path='/receive/file'))
Approach#1 - Error
drwxrwxrwx 1 0 0 0 12 Mar 13:18 ?
Traceback (most recent call last):
File "sftp_linux.py", line 32, in <module>
sftp_client.file("createfile","w+")
File "/usr/local/lib/python3.7/site-packages/paramiko/sftp_client.py", line 372, in open
t, msg = self._request(CMD_OPEN, filename, imode, attrblock)
File "/usr/local/lib/python3.7/site-packages/paramiko/sftp_client.py", line 813, in _request
return self._read_response(num)
File "/usr/local/lib/python3.7/site-packages/paramiko/sftp_client.py", line 865, in _read_response
self._convert_status(msg)
File "/usr/local/lib/python3.7/site-packages/paramiko/sftp_client.py", line 896, in _convert_status
raise IOError(errno.EACCES, text)
PermissionError: [Errno 13] Can not create createfile
Approach#2: using pysftp
import os
import sys
import pysftp
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
sftp_client = pysftp.Connection(host='abc.com', username='username', password='pwd', cnopts=cnopts,port=22)
print(sftp_client)
sftp_client.chdir ('/receive/file')
print(sftp_client.pwd)
print(sftp_client.listdir())
sftp_client.put('testingfile',preserve_mtime=True)
sftp_client.close()
Approach#2: using pysftp - error
[]
Traceback (most recent call last):
File "sftp_linux.py", line 15, in <module>
sftp_client.put('testingfile',preserve_mtime=True)
File "/usr/local/lib/python3.7/site-packages/pysftp/__init__.py", line 364, in put
confirm=confirm)
File "/usr/local/lib/python3.7/site-packages/paramiko/sftp_client.py", line 759, in put
return self.putfo(fl, remotepath, file_size, callback, confirm)
File "/usr/local/lib/python3.7/site-packages/paramiko/sftp_client.py", line 720, in putfo
s = self.stat(remotepath)
File "/usr/local/lib/python3.7/site-packages/paramiko/sftp_client.py", line 493, in stat
t, msg = self._request(CMD_STAT, path)
File "/usr/local/lib/python3.7/site-packages/paramiko/sftp_client.py", line 813, in _request
return self._read_response(num)
File "/usr/local/lib/python3.7/site-packages/paramiko/sftp_client.py", line 865, in _read_response
self._convert_status(msg)
File "/usr/local/lib/python3.7/site-packages/paramiko/sftp_client.py", line 894, in _convert_status
raise IOError(errno.ENOENT, text)
FileNotFoundError: [Errno 2] /receive/file/testingfile

PyCharm's code coverage not working due to Windows drive letters

I have a computer that has a C and D drive, where PyCharm, Python and the source code are installed on the D drive. I'm using Pipenv with PIPENV_VENV_IN_PROJECT set to enabled so that also ends up in the D drive. Despite all this, when running with code coverage enabled, I get this error:
Destroying test database for alias 'default'...
Traceback (most recent call last):
File "D:\Development\PyCharm\PyCharm 2019.1.1\helpers\coverage_runner\run_coverage.py", line 54, in <module>
main()
File "D:\Business\projectx\.venv\lib\site-packages\coverage\cmdline.py", line 770, in main
status = CoverageScript().command_line(argv)
File "D:\Business\projectx\.venv\lib\site-packages\coverage\cmdline.py", line 489, in command_line
return self.do_run(options, args)
File "D:\Business\projectx\.venv\lib\site-packages\coverage\cmdline.py", line 657, in do_run
self.coverage.save()
File "D:\Business\projectx\.venv\lib\site-packages\coverage\control.py", line 529, in save
data = self.get_data()
File "D:\Business\projectx\.venv\lib\site-packages\coverage\control.py", line 583, in get_data
if self._collector and self._collector.flush_data():
File "D:\Business\projectx\.venv\lib\site-packages\coverage\collector.py", line 425, in flush_data
self.covdata.add_lines(abs_file_dict(self.data))
File "D:\Business\projectx\.venv\lib\site-packages\coverage\sqldata.py", line 236, in add_lines
self._choose_lines_or_arcs(lines=True)
File "D:\Business\projectx\.venv\lib\site-packages\coverage\sqldata.py", line 279, in _choose_lines_or_arcs
with self._connect() as con:
File "D:\Business\projectx\.venv\lib\site-packages\coverage\sqldata.py", line 160, in _connect
self._create_db()
File "D:\Business\projectx\.venv\lib\site-packages\coverage\sqldata.py", line 117, in _create_db
with self._db:
File "D:\Business\projectx\.venv\lib\site-packages\coverage\sqldata.py", line 525, in __enter__
self.connect()
File "D:\Business\projectx\.venv\lib\site-packages\coverage\sqldata.py", line 510, in connect
filename = os.path.relpath(self.filename)
File "D:\Business\projectx\.venv\lib\ntpath.py", line 562, in relpath
path_drive, start_drive))
ValueError: path is on mount 'C:', start on mount 'D:'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\Development\PyCharm\PyCharm 2019.1.1\helpers\coverage_runner\run_coverage.py", line 58, in <module>
main(["xml", "-o", coverage_file + ".xml", "--ignore-errors"])
File "D:\Business\projectx\.venv\lib\site-packages\coverage\cmdline.py", line 770, in main
status = CoverageScript().command_line(argv)
File "D:\Business\projectx\.venv\lib\site-packages\coverage\cmdline.py", line 511, in command_line
self.coverage.load()
File "D:\Business\projectx\.venv\lib\site-packages\coverage\control.py", line 336, in load
self._data.read()
File "D:\Business\projectx\.venv\lib\site-packages\coverage\sqldata.py", line 409, in read
with self._connect(): # TODO: doesn't look right
File "D:\Business\projectx\.venv\lib\site-packages\coverage\sqldata.py", line 160, in _connect
self._create_db()
File "D:\Business\projectx\.venv\lib\site-packages\coverage\sqldata.py", line 117, in _create_db
with self._db:
File "D:\Business\projectx\.venv\lib\site-packages\coverage\sqldata.py", line 525, in __enter__
self.connect()
File "D:\Business\projectx\.venv\lib\site-packages\coverage\sqldata.py", line 510, in connect
filename = os.path.relpath(self.filename)
File "D:\Business\projectx\.venv\lib\ntpath.py", line 562, in relpath
path_drive, start_drive))
ValueError: path is on mount 'C:', start on mount 'D:'
Any ideas why?

Unable to launch Spyder due to FileNotFoundError

The error is the following:
Traceback (most recent call last):
File "C:\Users\Shubham\Anaconda3\lib\site-packages\spyder\app\mainwindow.py", line 3208, in main
mainwindow = run_spyder(app, options, args)
File "C:\Users\Shubham\Anaconda3\lib\site-packages\spyder\app\mainwindow.py", line 3084, in run_spyder
main.setup()
File "C:\Users\Shubham\Anaconda3\lib\site-packages\spyder\app\mainwindow.py", line 828, in setup
self.workingdirectory = WorkingDirectory(self, self.init_workdir, main=self)
File "C:\Users\Shubham\Anaconda3\lib\site-packages\spyder\plugins\workingdirectory.py", line 159, in __init__
self.chdir(workdir)
File "C:\Users\Shubham\Anaconda3\lib\site-packages\spyder\plugins\workingdirectory.py", line 296, in chdir
self.refresh_plugin()
File "C:\Users\Shubham\Anaconda3\lib\site-packages\spyder\plugins\workingdirectory.py", line 204, in refresh_plugin
self.save_wdhistory()
File "C:\Users\Shubham\Anaconda3\lib\site-packages\spyder\plugins\workingdirectory.py", line 234, in save_wdhistory
encoding.writelines(text, self.LOG_PATH)
File "C:\Users\Shubham\Anaconda3\lib\site-packages\spyder\utils\encoding.py", line 236, in writelines
return write(os.linesep.join(lines), filename, encoding, mode)
File "C:\Users\Shubham\Anaconda3\lib\site-packages\spyder\utils\encoding.py", line 227, in write
with open(filename, mode) as textfile:
FileNotFoundError: [Errno 2] No such file or directory: '%USERPROFILE\\.spyder-py3\\workingdir'
(Spyder maintainer here) This is a bug in Spyder and we'll fix it in our 3.3.0 version, to be released later in June/2018.

distutils.dir_util.copy_tree expects a file in destination before copying

I want to copy the contents of one directory to another, using copy_tree function for doing this. Below is my code:
copy_tree('/opt/Deployment/scripts/image', '/opt/Deployment/dist-packages/job-123')
A couple of files from the directory image are getting copied into job-123, but after that, I get this error:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/rpyc/core/protocol.py", line 305, in _dispatch_request
res = self._HANDLERS[handler](self, *args)
File "/usr/local/lib/python2.7/dist-packages/rpyc/core/protocol.py", line 535, in _handle_call
return self._local_objects[oid](*args, **dict(kwargs))
File "/usr/local/lib/python2.7/dist-packages/rpyc/core/service.py", line 157, in exposed_execute
execute(text, self.exposed_namespace)
File "<string>", line 2, in execute
File "<string>", line 5, in <module>
File "../installModel.py", line 17, in <module>
copy_tree('/opt/Deployment/scripts/image', '/opt/Deployment/dist-packages/job-123')
File "/usr/lib/python2.7/distutils/dir_util.py", line 163, in copy_tree
verbose=verbose, dry_run=dry_run))
File "/usr/lib/python2.7/distutils/dir_util.py", line 167, in copy_tree
dry_run=dry_run)
File "/usr/lib/python2.7/distutils/file_util.py", line 148, in copy_file
_copy_file_contents(src, dst)
File "/usr/lib/python2.7/distutils/file_util.py", line 44, in _copy_file_contents
fdst = open(dst, wb)
IOError: [Errno 2] No such file or directory: /opt/Deployment/dist-packages/job-123/lib/utils.pyc
Now this file utils.pyc is in the source directory in the lib subdirectory, it should get copied to the same subdirectory in the destination. Why is copy_tree expecting it to be there already and throwing an error?

Categories

Resources