0

I'm simply trying to alter a CSV file with Python.

When all the files were in the same dir, everything was fine.

Now that the input files are in a different dir than where the output files will be, everything blows up, apparently b/c the files do not exist?

I first found this: open() in Python does not create a file if it doesn't exist

Then I learned to change to the directory, which helped me loop over the CSVs in the target dir: Moving up one directory in Python

When I run the command: python KWRG.py ../Weekly\ Reports\ -\ Inbound/Search\ Activity/ 8/9/2021

I will get: Traceback (most recent call last): File "KWRG.py", line 15, in <module> with open(args.input, 'r') as in_file, open(args.output, 'w') as out_file: IsADirectoryError: [Errno 21] Is a directory: '../Weekly Reports - Inbound/Search Activity/'

Sorry If I'm missing the obvious here, but why is the file not being created in the directory that I'm pointing the script to (or at all for that matter)?

The code:

import csv
import argparse
import os

# Create a parser to take arguments
#...snip...

cur_dir = os.getcwd()
reports_dir = os.chdir(os.path.join(cur_dir, args.dir))

for csv_file in os.listdir(reports_dir):
    # Shorthand the name of the file
    #...snip...

    # Open the in and out files
    with open(csv_file, 'r') as in_file, open(f'{out_name}-Search-Activity-{args.date}.csv', 'w+') as out_file:
        # Re-arrange CSV
        # EOF

martineau
  • 119,623
  • 25
  • 170
  • 301
Snipe
  • 19
  • 7
  • What OS are you using? – MattDMo Aug 08 '21 at 18:05
  • Questions seeking debugging help (**"why isn't this code working?"**) should include the desired behavior, *a specific problem or error* and *the shortest code necessary* to reproduce it *as formatted text* (not images) **in the question itself**. Questions without **a clear problem statement** are not useful to other readers. See: [mre]. Specifically, please give the **full text** of the error/traceback you're getting, and also give a specific example of the program's input that is causing the error. – MattDMo Aug 08 '21 at 18:08
  • I'm using a Mac - Big Sur 11.5. – Snipe Aug 08 '21 at 18:09
  • You can't have file names with the `/` character in them. That's the directory delimiter. Use `-` or `.` or something else instead. – MattDMo Aug 08 '21 at 18:11
  • This script is neither pointed at, nor trying to create files with a "/" in the name. – Snipe Aug 08 '21 at 18:14
  • That's not what your error says: *`[Errno 2] No such file or directory: 'MDS-Search-Activity-8/9/2021'`* – MattDMo Aug 08 '21 at 18:17
  • Ouch I'm blind (and braindead apparently) - Thank you for that catch - My apologies for the blunt falsehood I stated. – Snipe Aug 08 '21 at 18:25
  • No worries. I added an answer to fix the current bug in the code. Cheers! – MattDMo Aug 08 '21 at 18:27

1 Answers1

0

Your problem is with this line:

reports_dir = os.chdir(os.path.join(cur_dir, args.dir))

os.chdir() doesn't return anything, it just performs the operation requested - changing the current working directory. From an interactive session with the REPL:

>>> import os
>>> result = os.chdir("/Users/mattdmo/")
>>> result
>>> 

For your purposes, all you need is

reports_dir = os.path.join(cur_dir, args.dir)

and you'll be all set.

MattDMo
  • 100,794
  • 21
  • 241
  • 231