简体   繁体   中英

Errors with Glob while outputting file names

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.

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM