I need to create a zip file using python. But unfortunately my code is not working.Is there any error on my part? - zip

**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

Related

Two errors when trying to open firefox using selenium (Python); System cannot find the file specified; Geckodriver executable needs to be in PATH

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()

Shutil raises OSError

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?

How to fix FileNotFoundError [Errno 2] File does not exist in Pyinstaller

I want to build an exe file using pyinstaller. Unfortunately, I cannot figure out, how to fix the following problem, which comes from Pandas library:
File "pandas\_libs\parsers.pyx", line 387, in
pandas._libs.parsers.TextReader.__cinit__
File "pandas\_libs\parsers.pyx", line 705, in
pandas._libs.parsers.TextReader._setup_parser_source
FileNotFoundError: [Errno 2] File b'data/test/file1.csv' does not exist:
b'data/test/file1.csv'
I've tried to include this folder to datas, but it does not help. All source files and data folder are located in one folder.
files_to_add= [
( 'data', 'data' )
]
a = Analysis(['my_script.py'],
pathex=['C:\\Users\\...'],
binaries=[],
datas=files_to_add,

shutil.move conditional errors

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.

Mac: saving .exe to PATH for selenium web driver

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

Resources