Opening .DBF files as read-only with the dbf Python module - python

First of all, the dbf module is great. I've been using it with great success.
I'm trying to open a dbf file on a network share which is a read-only file system. When I try to open it like this, I get an error which says that the .dbf file is read-only.
thisTable = dbf.Table('/volumes/readOnlyVolume/thisFile.dbf')
thisTable.open()
Looking at the documentation, it looks like there's a way to open a table in read-only mode, but I can't figure it out. If you have a second, would you be able to help me out?
Thanks!
Kyle

Cool, thanks! :)
At this point, you need to specify the open mode when you call thisTable.open(), like this:
thisTable.open(mode='read-only')
or
thisTable.open(mode=dbf.READ_ONLY)
Oh, and here's the PyPI link to the module.

Assuming you're using this module, the magic incantation to open read only is:
dbf1 = Dbf()
dbf1.openFile('county.dbf', readOnly=1)
Hope that helped, if not, do add more detail to your question.

Related

How to export a rawEDF file to my computer

so basically I am manipulating edf files using mne and pyedflib libraries in python. As long as I am applying many changes on my edf_file, I need to export my edf file after each modification. I tried many commands with mne and pyedflib but none of them seems to work. Does anyone have a solution please?
Any kind of help would be much appreciated!
Since you do not specify what have you tried and what you have not tried exactly I will point to the doc that it seems to fit in your case and an example directly from the lib you use.
I assume you are using the writer so here the doc for pyedflib:
EDF file writer
and you should check directly this example from the pyedflib repository to adapt to your needs:
https://github.com/holgern/pyedflib/blob/master/demo/writeEDFFile.py
I think the example in the repo should give you a good idea.

Xlwings won't close book after saving

I am currently trying to use xlwings to open a book and update it's links, then save and close. The relevant code I am using is:
import os
import xlwings as xw
app=xw.App(add_book=False)
app.display_alerts=False
for file in os.scandir(dirname):
if (file.name.endswith("Unposted Summary.xlsm")):
path=file.path
tmp=app.books.api.Open(path,UpdateLinks=True)
tmp.save(path)
app.quit()
After having read the documentation several times and using several different methods such as app.quit(), app.kill(), book.close(), etc... I have been unable to get xlwings to close the current book after saving it, so I haven't even approached the question of whether the links are updating properly or not.
I'm guessing the problem is coming from how I'm opening the books. If so, I don't know the syntax to close them.
I don't usually use xlwings, but from what I understand app.books.api.Open calls and returns the COM object, from where I don't even think tmp.save(...) would work (at least not in my case).
A better option would be work directly with xw.Book wrapper instead without the api call:
for file in os.scandir(dirname):
if (file.name.endswith("Unposted Summary.xlsm")):
tmp=app.books.open(file.path, update_links=True)
tmp.save()
tmp.close()
I would also advise you to exercise os.path.abspath and keep in mind your working directory while looping though dirname.

Loading file into Jupiter Notebook

Recently followed a short Python bootcamp and have been trying to work on it myself a bit more, but get stuck at the start. We need to upload the data, but for some reason I can't get it to work. In the example it was done like this:
Correct way apparently
So I figured it was just about the file path, so tried to copy that but don't get it to work
My wrong attempt
Any help would be thoroughly appreciated!
Did you try saving the notebook in the same directory where the CSV file exists and use a relative path to import the CSV?
data/titatic.csv
or
titatic.csv

How to get the modified date of an .xls file using xlrd

Let me try to improve this question, as it is still valid for me.
I have been using Openpyxl to read Excel files for a while. Now I need to extend the capability of my script to handle "legacy" Excel files that are not supported by Openpyxl. For this I use xlrd and xlutils.
On issue I have yet to solve is how to get the modified date of an .xls file in the case where I don't have the path. Using Openpyxl, I can get this as Workbook.properties.modified, as a datetime object. With xlrd I am struggling. A workaround would be to figure out the path to the file (which may be input to my function as a file like object) and to use os.path.getmtime, but I am not sure if that is equivalent.
Any help is appreciated!
I just came across your post while searching for a solution myself. Seems like
wb = xlrd.open_workbook(filename=fn)
wb.props.get('modified')
does the job.
xlrd==1.2.0

python pandas to excel error

I have a dataframe that I want to export to Excel. I'm new to python and pandas so I need some help on this simple task.
df2.to_excel('C:\BT\stack_test3.xlsx')
Error message:
IOError: [Errno 13] Permission denied: 'C:\BT\stack_test3.xlsx'
You path is incorrect, because you have not escaped the slashes it thinks you are trying to write to the root of c: drive use the following:
df2.to_excel(r'C:\BT\stack_test3.xlsx')
The r makes the path a raw string and means you do not need to escape the slashes
Edit
It seems that there is some error with openpyxl as using
df2.to_excel(r'C:\BT\stack_test3.xls')
works which uses xlwt, I don't know enough about those packages so it could be either a permissions problem with openpyxl which I have not been able to find anything about or a bug.
I had an identical problem. Turns out it's because I had left the Excel file open whilst I was trying to write to it. Apparently it doesn't like that. If you have it open try closing it.
To confirm...in case future readers stumble in this page...before complicating things make sure that the excel file you are trying to save is not already open or to be safe.
Just close all of excel and try save it again.
That should do it.
You should write to another drive like 'D:' because in Windows Vista or above that you have no permission to write to 'C:\' and you have no reason to earn the permission.
After closing all instances of excel and running
python code works.

Categories