954,557 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Is this possible in Python?

Is it possible in python to open all files in a directory?

this only opens a particular file:

import os

path = "/Desktop/directory/file.odt"

temp = os.system("open " + path)


how about this? am i doing it right?

import os

mypath = "/Desktop/directory"

temp = os.listdir(mypath) 

temp = [os.path.join(mypath, i) 
for i in temp:
    if os.path.splitext(f)[1] in mytemp]

os.system("open " + temp)

Advises and comments are very much appreciated. Thank you

bettersaid
Light Poster
34 posts since Jul 2010
Reputation Points: 10
Solved Threads: 2
 

I'd imagine it'd be something like

import os
import glob # If you are running *nix. Looks like you are

mypath = '/Desktop/directory/'
files_in_directory = glob.glob(mypath) # Creates a list of all the files in your directory
'''This a comment. If you only wanted, say text files to show up in 
the list above, use this. 
files_in_directory = glob.glob('%s*.txt'%mypath)'''
for file in files_in_directory: # Go through the files
    os.system('open'+file) # Open them



But I am not near my computer that has Python on it so hope this helps a bit

redyugi
Junior Poster in Training
80 posts since Jul 2009
Reputation Points: 15
Solved Threads: 30
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: