0

When I run the specified code I get the specified Error message.

Code:

import tkinter
import csv
import pandas as pd

from tkinter import Tk
from tkinter.filedialog import askopenfilename

Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
filename = askopenfilename() # show an "Open" dialog box and return the path to the selected file
print(filename)

df1 = pd.read_csv(filename)

df2 = df1.query("STATE != 'NY'") # stores records not in NY

df3 = df1[df1["FIRST_NAME"].str.contains(" ", regex=True)] # stores records that have a space in the first name

dferror = [df2, df3]
dferror = pd.concat(dferror).drop_duplicates().reset_index() # merges dataframes, drops duplicates


dferror.to_csv('C:\Errors.csv', index=False)

Error:

Traceback (most recent call last):
  File "C:/Work/Python/EDI/NYColonial.py", line 23, in <module>
    dferror.to_csv('C:\Errors.csv', index=False)
  File "C:\Program Files (x86)\Python37-32\lib\site-packages\pandas\core\frame.py", line 1745, in to_csv
    formatter.save()
  File "C:\Program Files (x86)\Python37-32\lib\site-packages\pandas\io\formats\csvs.py", line 156, in save
    compression=self.compression)
  File "C:\Program Files (x86)\Python37-32\lib\site-packages\pandas\io\common.py", line 400, in _get_handle
    f = open(path_or_buf, mode, encoding=encoding)
OSError: [Errno 22] Invalid argument: 'C:\\Errors.csv'

Process finished with exit code 1

If I removed the C:\ from C:\Errors.csv it will output my csv file where my python script is located, which is not what I want it to do.

I want to be able to tell my script to output the Errors.csv file to a specific location.

How do I specify that absolute file path? I've done some research but haven't been able to find my answer.

Rory Daulton
  • 21,934
  • 6
  • 42
  • 50
Kalgaer
  • 33
  • 1
  • 1
  • 6

1 Answers1

0

You can construct the full path using os path library.

import os

target_filaname = os.path.join('c:', 'Errors.csv')
dferror.to_csv(target_filename, index=False)
Raja Sattiraju
  • 1,262
  • 1
  • 20
  • 42
  • So I tried this and got a "PermissionError: [Errno 13] Permission denied: 'c:Errors.csv'" Which is a different error! I just started working on this computer so it is possible there are permission issues I still need to work out, however "C:Errors.csv" does not appear to be a valid file path to me? – Kalgaer Jul 24 '18 at 20:35