Issue
I'm trying to import a csv file into my Python project with PyQt5's QFileDialog. All I need is for it to copy a file from one location and paste it in another yet I can't seem to figure it out.
def open(self):
home_dir = str(Path.home())
fname = QFileDialog.getOpenFileName(self, 'Open file', home_dir)
# if file selected import into folder
if fname[0]:
print(fname)
with open(fname, 'r') as fobj, open('Data/Inventory.csv', 'wb') as f:
data = fobj.read()
df = pd.read_csv(data)
f.write(df)
So far this is all I have. I had a version before that created a blank file in the desired directory but that's obviously not enough.
Solution
I figured it out so I thought I'd share what I found.
def open(self):
home_dir = str(Path.home())
fname = QFileDialog.getOpenFileName(self, 'Open file', home_dir)
# if file selected import into folder
if fname[0]:
shutil.copyfile(fname[0], 'Data\Inventory.csv')
That's literally it.
Answered By - Chris
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.