-1

I am combining two questions here because they are related to each other.

Question 1: I am trying to use glob to open all the files in a folder but it is giving me "Syntax Error". I am using Python 3.xx. Has the syntax changed for Python 3.xx?

Error Message:

File "multiple_files.py", line 29
files = glob.glob(/src/xyz/rte/folder/)

SyntaxError: invalid syntax

Code:

import csv
import os
import glob
from pandas import DataFrame, read_csv

    #extracting
files = glob.glob(/src/xyz/rte/folder/)
for fle in files:
    with open (fle) as f:
        print("output" + fle)                   
f_read.close()

Question 2: I want to read input files, append "output" to the names and print out the names of the files. How can I do that?

Example: Input file name would be - xyz.csv and the code should print output_xyz.csv .

Your help is appreciated.

2
  • 2
    The filepath needs to be a string - files = glob.glob("/src/xyz/rte/folder/")
    – DavidG
    Mar 19, 2018 at 22:06
  • Thanks! Now I am getting a different error - IsADirectoryError: [Errno 21] Is a directory: "/src/xyz/rte/folder/)". Please comment!
    – New User
    Mar 19, 2018 at 22:24

2 Answers 2

1

Your first problem is that strings, including pathnames, need to be in quotes. This:

files = glob.glob(/src/xyz/rte/folder/)

… is trying to divide a bunch of variables together, but the leftmost and rightmost divisions are missing operands, so you've confused the parser. What you want is this:

files = glob.glob('/src/xyz/rte/folder/')

Your next problem is that this glob pattern doesn't have any globs in it, so the only thing it's going to match is the directory itself.

That's perfectly legal, but kind of useless.

And then you try to open each match as a text file. Which you can't do with a directory, hence the IsADirectoryError.

The answer here is less obvious, because it's not clear what you want.

  • Maybe you just wanted all of the files in that directory? In that case, you don't want glob.glob, you want listdir (or maybe scandir): os.listdir('/src/xyz/rte/folder/').

  • Maybe you wanted all of the files in that directory or any of its subdirectories? In that case, you could do it with rglob, but os.walk is probably clearer.

  • Maybe you did want all the files in that directory that match some pattern, so glob.glob is right—but in that case, you need to specify what that pattern is. For example, if you wanted all .csv files, that would be glob.glob('/src/xyz/rte/folder/*.csv').


Finally, you say "I want to read input files, append "output" to the names and print out the names of the files". Why do you want to read the files if you're not doing anything with the contents? You can do that, of course, but it seems pretty wasteful. If you just want to print out the filenames with output appended, that's easy:

for filename in os.listdir('/src/xyz/rte/folder/'):
    print('output'+filename)
2
  • Definitely, I am doing lot more to the files. I just wrote "print" because I wanted to simplify my questions. I want to create file with filenames "output_<input_filename>". I will process these files later. Your answer works but could you tell me how to append the file names and create files with "output" in the front?
    – New User
    Mar 19, 2018 at 23:00
  • @NewUser The part at the end shows how to do that. And you were already doing it in your own code. Although if you want an underscore there, you have to include it in the string: 'output_' + filename.
    – abarnert
    Mar 19, 2018 at 23:13
0

This works in http://pyfiddle.io:

Doku: https://docs.python.org/3/library/glob.html

import csv
import os
import glob 

# create some files
for n in ["a","b","c","d"]:
    with open('{}.txt'.format(n),"w") as f:
        f.write(n) 

print("\nFiles before")

# get all files
files = glob.glob("./*.*")

for fle in files:
    print(fle)      # print file
    path,fileName = os.path.split(fle)  # split name from path

    # open file for read and second one for write with modified name
    with open (fle) as f,open('{}{}output_{}'.format(path,os.sep, fileName),"w") as w:
        content = f.read()            # read all
        w.write(content.upper())      # write all modified

# check files afterwards
print("\nFiles after")
files = glob.glob("./*.*")  # pattern for all files
for fle in files:
    print(fle)                              

Output:

Files before
./d.txt
./main.py
./c.txt
./b.txt
./a.txt

Files after
./d.txt
./output_c.txt
./output_d.txt
./main.py
./output_main.py
./c.txt
./b.txt
./output_b.txt
./a.txt
./output_a.txt

I am on windows and would use os.walk (Doku) instead.

for d,subdirs,files in os.walk("./"): # deconstruct returned aktDir, all subdirs, files
    print("AktDir:", d)
    print("Subdirs:", subdirs)
    print("Files:", files)

Output:

AktDir: ./
Subdirs: []
Files: ['d.txt', 'output_c.txt', 'output_d.txt', 'main.py', 'output_main.py', 
        'c.txt', 'b.txt', 'output_b.txt', 'a.txt', 'output_a.txt'] 

It also recurses into subdirs.

1
  • @NewUser thats why I tried it on pyfiddle.io - its a web based python fiddle that runs on some linux-derivate server ;o) Mar 20, 2018 at 6:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.