/anaconda3/lib/python3.5/zipfile.py error when attempting to write to an Excel file

Hi, I'm running a python script using Spyder (Python 3.5) on OSX 10.13.6 and keep on running into file not found errors when attempting to copy data into a Microsoft Excel file. It looks like a permissions issue, but I don't know how to resolve. Any help would be much appreciated,

import urllib.request
import pandas as pd
import sys
import os

sys.path.append('/Users/bhav\ 1/Documents/keepbak2/python')

from html_table_parser import HTMLTableParser

target = 'weblink'

#get website config
req = urllib.request.Request(url=target)
f = urllib.request.urlopen(req)
xhtml = f.read().decode('utf-8')

#instantiate he parser and feed it
p = HTMLTableParser()

#write the table into python list

cwd = os.getcwd()  # Get the current working directory (cwd)

Table_List = p.tables

# Use the Pandas from_dict method to read the tables into
# a Pandas data frame

df = pd.DataFrame.from_dict(Table_List[0])

#CountryDied_Stats.to_excel('/Users/bhav\ 1/Documents/keepbak2/python/countrydiedtest.xlsx',
#                           na_rep='',
#                           float_format=None, columns=None,
#                           header=True, index=True,
#                           index_label=None, startrow=0,
#                           startcol=0, engine=None, merge_cells=True,
#                           encoding=None, inf_rep='inf', verbose=True)

#xlfile = 'countrydiedtest.xlsx'
xlfile = '/Users/bhav\ 1/Documents/keepbak2/python/countrydiedtest.xlsx '

cwd = os.getcwd()  # Get the current working directory (cwd)
files = os.listdir(cwd)  # Get all the files in that directory
print("Files in '%s': %s" % (cwd, files))

# open file for reading
fn = input(xlfile)
if os.path.exists(fn):
#    fh = open(fn, "r")
    print("row 56 open(fn, r)")
#    fh = open(fn, "w")
    print(" row 59 open(fn, w)")

# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter(fn, engine='xlsxwriter')

# Convert the dataframe to an XlsxWriter Excel object.
df.to_excel(writer, sheet_name='Sheet1')

# Close the Pandas Excel writer and output the Excel file.

Open in new window

runfile('/Users/bhav 1/Documents/keepbak2/python/html-table-parser/scrape1.py', wdir='/Users/bhav 1/Documents/keepbak2/python/html-table-parser')
Reloaded modules: html_table_parser.parser, html_table_parser
/Users/bhav 1/Documents/keepbak2/python/html-table-parser
Files in '/Users/bhav 1/Documents/keepbak2/python/html-table-parser': ['countrydiedtest.xlsx', 'LICENSE', 'README.md', 'scrape1.py', '.gitignore', '.python-version', 'html_table_parser', 'example_of_usage.py', 'html_table_converter']

/Users/bhav 1/Documents/keepbak2/python/countrydiedtest.xlsx
 row 59 open(fn, w)
Traceback (most recent call last):

  File "<ipython-input-3-e281686b8449>", line 1, in <module>
    runfile('/Users/bhav 1/Documents/keepbak2/python/html-table-parser/scrape1.py', wdir='/Users/bhav 1/Documents/keepbak2/python/html-table-parser')

  File "/anaconda3/lib/python3.5/site-packages/spyder_kernels/customize/spydercustomize.py", line 668, in runfile
    execfile(filename, namespace)

  File "/anaconda3/lib/python3.5/site-packages/spyder_kernels/customize/spydercustomize.py", line 108, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "/Users/bhav 1/Documents/keepbak2/python/html-table-parser/scrape1.py", line 73, in <module>

  File "/anaconda3/lib/python3.5/site-packages/pandas/io/excel.py", line 1732, in save
    return self.book.close()

  File "/anaconda3/lib/python3.5/site-packages/xlsxwriter/workbook.py", line 306, in close

  File "/anaconda3/lib/python3.5/site-packages/xlsxwriter/workbook.py", line 635, in _store_workbook

  File "/anaconda3/lib/python3.5/zipfile.py", line 1009, in __init__
    self.fp = io.open(file, filemode)

FileNotFoundError: [Errno 2] No such file or directory: ''
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

My bet is on the space in the string "bhav\ 1" to be a problem.
Try duplicating backslash llike this: "bhav\\ 1" in the XLS string path.
python is not a shell. So normally you do not have to (must not) escape spaces.

you should use '/Users/bhav 1/Documents/keepbak2/python/countrydiedtest.xlsx'

but to quickly test what's wrong I suggest to run following code snippet:

import os

fname = '/Users/bhav 1/Documents/keepbak2/python/countrydiedtest.xlsx'
while True:
    parent_dir = os.path.dirname(fname)
    if fname == parent_dir:
    if os.path.isdir(parent_dir):
        print("Directory %s exists" % parent_dir)
        print("Directory %s does not exist" % parent_dir)
    fname = parent_dir

Open in new window

patelbg2001Author Commented:
changing the file path resolved my issue.

#xlfile = 'countrydiedtest.xlsx'
xlfile = "/Users/bhav 1/Documents/dump/countrydiedtest.xlsx"

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

This is what I suggested in my answer:

python is not a shell. So normally you do not have to (must not) escape spaces.

you should use '/Users/bhav 1/Documents/keepbak2/python/countrydiedtest.xlsx'

So I'm a little surprised, that you accept your and not my solution
patelbg2001Author Commented:
Agreed. It was my error. Thanks.
no problem. Happy coding
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.