Python Forum
Unable to open files without file extension
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unable to open files without file extension
#1
I am working with the Enron emails and they do not have a file extension. I can get the first set of code to work but I cannot in the second (see error I am getting there):

This code works (cwd stands for current working directory):
for file in glob.glob(cwd + "/*" ):
    f = open(file, "r")
    print(f.read())


This same code as used below does not work. I get a 'permission denied' error since, I believe, the function is trying to access the dir itself rather than the files within it. Note that I have also tried using a 'if file.endswith("*.*")' but this will not recognize the files since there is no extension:

for dirpath, dirs, files in os.walk(cwd):
    for file in glob.glob(dirpath + "/*"): # I have also tried this as 'for file in glob.glob("/*")' and 'for file in glob.glob("*")'
        path = os.path.join(dirpath, file)
        print(os.getcwd())
        f = open(file, "r")
        text = f.read()
        f.close()


Any idea on how to get this to work would be appreciated.
Reply
#2
(Dec-24-2016, 03:03 AM)pstarrett Wrote: I can get the first set of code to work but I cannot in the second (see error I am getting there):
You missed to post the error you get. Please, give us the full traceback.

You should use the code tags.

victor@jerry:/media/storage/Audio $ import glob

victor@jerry:/media/storage/Audio $ import pprint

victor@jerry:/media/storage/Audio $ pprint.pprint(glob.glob("*.mp3"))

["n'to   -trauma (worakls remix).mp3"]
You don't provide the right arguments for glob.glob().

victor@jerry:/media/storage/Audio $ help(glob.glob)
Help on function glob in module glob:

glob(pathname, *, recursive=False)
    Return a list of paths matching a pathname pattern.
    
    The pattern may contain simple shell-style wildcards a la
    fnmatch. However, unlike fnmatch, filenames starting with a
    dot are special cases that are not matched by '*' and '?'
    patterns.
    
    If recursive is true, the pattern '**' will match any files and
    zero or more directories and subdirectories.
In your case it should be glob.glob(cwd, "*")
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
Thank you for the answer and it is in the direction but ultimately got me to the right place. I am using Python 3.4 right now so that version of the glob library is used in 3.5. I am required to use the os.walk version - future reviewers of this post will find the below link very useful for this issue in either version of Python:

http://stackoverflow.com/questions/21865...-in-python
Reply
#4
Why are you using os.walk and then glob.glob() bellow since os.walk() is good enough to get the files. But it run thru all the directories.

import os.path

for root_dir, dir, files in os.walk(top_dir):
    if os.path.abspath(top_dir) != os.path.abspath(root_dir):
        break

    if files:
        for file in files:
            print(file)
This will print the file names in all top_dir directory and is going to stop once it attend to go to another dir in the tree.

And why does your post is not in "Homework"?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Open files in an existing window instead of new Kostov 2 259 Apr-13-2024, 07:22 AM
Last Post: Kostov
  Open/save file on Android frohr 0 312 Jan-24-2024, 06:28 PM
Last Post: frohr
  file open "file not found error" shanoger 8 1,082 Dec-14-2023, 08:03 AM
Last Post: shanoger
  open python files in other drive akbarza 1 677 Aug-24-2023, 01:23 PM
Last Post: deanhystad
  How can i combine these two functions so i only open the file once? cubangt 4 851 Aug-14-2023, 05:04 PM
Last Post: snippsat
  I cannot able open a file in python ? ted 5 3,277 Feb-11-2023, 02:38 AM
Last Post: ted
  testing an open file Skaperen 7 1,356 Dec-20-2022, 02:19 AM
Last Post: Skaperen
  I get an FileNotFouerror while try to open(file,"rt"). My goal is to replace str decoded 1 1,395 May-06-2022, 01:44 PM
Last Post: Larz60+
  select Eof extension files based on text list of filenames with if condition RolanRoll 1 1,507 Apr-04-2022, 09:29 PM
Last Post: Larz60+
  Dynamic File Name to a shared folder with open command in python sjcsvatt 9 6,012 Jan-07-2022, 04:55 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020