I have a short python script (called VaultTransferScript.py) that should transfer a zip file from one machine to another. The destination machine is a mapped network-attached-storage machine, which I have assigned to be the Z: drive.
The script is:
import shutil
import os
from datetime import datetime
time_stamp = datetime.now().strftime('%Y-%m-%d_%H_%M')
title_str = 'VaultBackup.zip'
name = time_stamp + title_str
shutil.move('C:\\Users\\Hawking\\Desktop\\VaultBackups\\MyBackup.zip',
os.path.join('Z:\\VaultBackups\\'+name))
I can run this script from the notepad++ run facility, using
cmd /C python "$(FULL_CURRENT_PATH)"
But running it in a batch script as:
echo off
C:\Users\Hawking\AppData\Local\Programs\Python\Python37-32\python.exe C:\Users\Hawking\Desktop\VaultBackupTransfer.py
results in this:
C:\Users\Hawking\Desktop>echo off
Traceback (most recent call last):
File "C:\Users\Hawking\AppData\Local\Programs\Python\Python37-32\lib\shutil.py", line 557, in move
os.rename(src, real_dst)
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:\\Users\\Hawking\\Desktop\\VaultBackups\\MyBackup.zip' -> 'Z:\\VaultBackups\\2018-09-21_14_30VaultBackup.zip'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Hawking\Desktop\VaultBackupTransfer.py", line 7, in <module>
shutil.move('C:\\Users\\Hawking\\Desktop\\VaultBackups\\MyBackup.zip', os.path.join('Z:\\VaultBackups\\'+name))
File "C:\Users\Hawking\AppData\Local\Programs\Python\Python37-32\lib\shutil.py", line 571, in move
copy_function(src, real_dst)
File "C:\Users\Hawking\AppData\Local\Programs\Python\Python37-32\lib\shutil.py", line 257, in copy2
copyfile(src, dst, follow_symlinks=follow_symlinks)
File "C:\Users\Hawking\AppData\Local\Programs\Python\Python37-32\lib\shutil.py", line 121, in copyfile
with open(dst, 'wb') as fdst:
FileNotFoundError: [Errno 2] No such file or directory: 'Z:\\VaultBackups\\2018-09-21_14_30VaultBackup.zip'
WHat is the difference in how I invoke the python script, and why does it error out from the batch script, but not notepad++?
You might be running the Python program with different user permissions in Notepad++ versus the command prompt. Alternatively, another Python VM might be used. Although, nothing in particular makes me think that the later is true.
Related
import time
from selenium import webdriver
path = input("enter filepath: ")
driver = webdriver.Firefox(path)
driver.get("htpps://google.com")
This is the part of my code seemingly causing the error, I have been able to find any answers online. The code used to work when using r"c:\users\ellio\desktop\main" however this has since stopped working too, only without the top error. I have added the folder containg geckodriver to path and added the geckodriver exe to the same file as my code, but nothing seems to work.
Thanks in advance for any help.
Here are the errors
Traceback (most recent call last):
File "C:\Users\ellio\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\common\service.py", line 72, in start
self.process = subprocess.Popen(cmd, env=self.env,
File "C:\Users\ellio\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 951, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\ellio\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 1420, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:\Users\ellio\Desktop\main\app.py", line 18, in <module>
driver = webdriver.Firefox(path)
File "C:\Users\ellio\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 164, in __init__
self.service.start()
File "C:\Users\ellio\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\common\service.py", line 81, in start
raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
give a specific path of your driver
from selenium import webdriver
driver = webdriver.Chrome('D:\\chrome\\chromedriver.exe') #give location of driver you download
driver.get('http://www.google.com/');
time.sleep(5)
search_box = driver.find_element_by_name('q')
search_box.send_keys('ChromeDriver')
search_box.submit()
time.sleep(5)
driver.quit()
In my webscraping project I collect different types of data from the internet, which are added to an excel file.
Until recently, my back up method of this excel file consisted of copying it to a different folder, using Shutil. I updated the script in a more proper way so that the filename includes the corresponding year and month too.
Example of original file: data.xlsx
Name of backup file: 2021-03_data.xlsx
No magic whatsoever, but ever since, the script raises an OS Error, stating that the operation is not supported. Interestingly, the file is copied perfectly... But the error bugs me since it breaks the script and does not execute what follows.
This is the code:
#Export to Excel
final_df.to_excel(last_search, index=False)
#Backup
yearBackup = str(datetime.now().year)
monthBackup = str('{:02d}'.format(datetime.now().month)) #2 digits
pathBackup = Path(os.path.dirname(__file__), 'backup' , f'{yearBackup}-{monthBackup}_data.xlsx')
shutil.copy(last_search, pathBackup)
The error message refers to line 146:
shutil.copy(last_search, pathBackup)
This is the error:
Traceback (most recent call last):
File "/run/user/1000/gvfs/smb-share:server=snowserver,share=home/Drive/Drive/Python/Webscraping/projects/online/online-data.py", line 146, in <module>
shutil.copy(last_search, pathBackup)
File "/usr/lib/python3.8/shutil.py", line 416, in copy
copymode(src, dst, follow_symlinks=follow_symlinks)
File "/usr/lib/python3.8/shutil.py", line 305, in copymode
chmod_func(dst, stat.S_IMODE(st.st_mode))
OSError: [Errno 95] Operation not supported: '/run/user/1000/gvfs/smb-share:server=snowserver,share=home/Drive/Drive/Python/Webscraping/projects/online/backup/2021-03_data.xlsx'
Does anyone know how to solve this?
**So i wish to create a zip file named new.zip using python which also contains a text file called sample.txt. But after running the necessary code on my python editor i get the error as :
Traceback (most recent call last):
File "C:/Users/User/PycharmProjects/PYTHON_CODE/creatingZipFile.py", line 5, in
newZip.write('sample.txt', compress_type=zipfile.ZIP_DEFLATED)
File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\zipfile.py", line 1713, in write
zinfo = ZipInfo.from_file(filename, arcname)
File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\zipfile.py", line 506, in from_file
st = os.stat(filename)
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'sample.txt'
My python code is as below :
import os, zipfile
os.chdir("F:\\")
newZip = zipfile.ZipFile('new.zip', 'a')
newZip.write('sample.txt', compress_type=zipfile.ZIP_DEFLATED)
newZip.close()
PLEASE SOMEONE HELP ME REGARDING THIS ISSUE.**
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'sample.txt'
will occur if the file is not present in the specified path, can you check if sample.txt is actually present in the path F:\sample.txt
I am not a professional developer... trying to save chromedriver to PATH variable so i can use selenium with python.
following this page for instructions on this page
https://medium.com/#himanshuagarwal1395/setting-up-environment-variables-in-macos-sierra-f5978369b255
I open bash_profile with nano .bash_profile
I made a directory user/mysusername/selenium/
then saved my file chromedriver in this directory with the python script i and using selenium with.
I have tried to save my path to the chromedriver.exe file as such
export PATH="/User/baldoa/selenium/ChromeDriver.exe:$PATH"
I follow the steps to save appropriately according to this page. I am not sure
i still get the following error :
a8206602016e:selenium baldoa$ python destruction.py
Traceback (most recent call last):
File "/Users/baldoa/.pyenv/versions/3.6.5/lib/python3.6/site-packages/selenium
/webdriver/common/service.py", line 76, in start
stdin=PIPE)
File "/Users/baldoa/.pyenv/versions/3.6.5/lib/python3.6/subprocess.py", line
709, in __init__
restore_signals, start_new_session)
File "/Users/baldoa/.pyenv/versions/3.6.5/lib/python3.6/subprocess.py", line
1344, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'C:/users/baldoa
/selenium/chromedriver.exe': 'C:/users/baldoa/selenium/chromedriver.exe'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "destruction.py", line 7, in <module>
browser = webdriver.Chrome("""C:/users/baldoa/selenium/chromedriver.exe""")
File "/Users/baldoa/.pyenv/versions/3.6.5/lib/python3.6/site-packages/selenium
/webdriver/chrome/webdriver.py", line 68, in __init__
self.service.start()
File "/Users/baldoa/.pyenv/versions/3.6.5/lib/python3.6/site-packages/selenium
/webdriver/common/service.py", line 83, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'chromedriver.exe'
executable needs to be in PATH. Please see https://sites.google.com
/a/chromium.org/chromedriver/home
I'm trying to upload couple of .zip files with size ranging between 20 to 30 MBs using python's sftp module 'Paramiko'. I'm able to successfully connect to the sftp server, and also able to list the contents in the destination directory. But while attempting to upload files from my ec2 linux machine, the request is taking too much time and then terminating with EOFError(). Below is the code,
>>> import paramiko
>>> import os
>>> ftp_host='*****'
>>> ftp_username='***'
>>> ftp_password='**'
>>> ssh_client=paramiko.SSHClient()
>>> ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
>>> ssh_client.connect(hostname=ftp_host,username=ftp_username,password=ftp_password, timeout=500)
>>> ftp_client=ssh_client.open_sftp()
>>> ftp_client.chdir('/inbound/')
>>> ftp_client.listdir()
[u't5-file-1.zip', u'edp_r.zip', u'T5_Transaction_Sample.gz']
>>> ftp_client.put("/path-to-zip-files-on-local/edp_revenue.zip","/inbound/edp_revenue.zip")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/paramiko/sftp_client.py", line 669, in put
return self.putfo(fl, remotepath, file_size, callback, confirm)
File "/usr/lib/python2.7/dist-packages/paramiko/sftp_client.py", line 626, in putfo
fr.write(data)
File "/usr/lib/python2.7/dist-packages/paramiko/file.py", line 331, in write
self._write_all(data)
File "/usr/lib/python2.7/dist-packages/paramiko/file.py", line 448, in _write_all
count = self._write(data)
File "/usr/lib/python2.7/dist-packages/paramiko/sftp_file.py", line 176, in _write
self._reqs.append(self.sftp._async_request(type(None), CMD_WRITE, self.handle, long(self._realpos), data[:chunk]))
File "/usr/lib/python2.7/dist-packages/paramiko/sftp_client.py", line 750, in _async_request
self._send_packet(t, msg)
File "/usr/lib/python2.7/dist-packages/paramiko/sftp.py", line 170, in _send_packet
self._write_all(out)
File "/usr/lib/python2.7/dist-packages/paramiko/sftp.py", line 135, in _write_all
raise EOFError()
EOFError
When I'm trying the same thing using curl command I'm easily able to do and just in 20 seconds. What am I missing here? Is it the size which is making the request to timed out or anything else.
It seems I was using an older version of paramiko on my linux server. I upgraded it using,
pip install --upgraded paramiko
and then logged everything in a log file,
paramiko.util.log_to_file('/path_to_log_file/paramiko.log')
just to capture every detail. Now it seems to be working pretty fast and correct.