Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 558991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:11:24+00:00 2026-05-13T12:11:24+00:00

I have this code: def GetSteamAccts(): #Get list of steam logins on this computer.

  • 0

I have this code:

def GetSteamAccts(): #Get list of steam logins on this computer.
    templist = []
    Steamapp_Folders = ["C:\\Program Files (x86)\\Steam\\steamapps\\", "C:\\Program Files\\Steam\\steamapps\\"] #Check both of these directories.
    for SF_i in range(len(Steamapp_Folders)):
        if os.path.exists(Steamapp_Folders[SF_i]): #If the directory even exists...
            Steam_AppDir_Items = os.listdir(Steamapp_Folders[SF_i]) #List items under steam install directory.
            for S_AD_i in range(len(Steam_AppDir_Items)): #Make sure the user doesn't have any files in here...
                if os.path.isdir(Steamapp_Folders + Steam_AppDir_Items[S_AD_i]): #If our path is a directory...
                    templist.append(Steam_AppDir_Items[S_AD_i])  #Add it to our list of logins.
                                                                 #(If some idiot puts extra folders in here,
                                                                 #it's their own damn fault when it shows on the list.)
    return templist #Return a (not so) properly filtered list of steam logins.

My problem is, it looks AWFULLY ugly to me. I made a list of 2 paths (only 1 will ever exist), loop over those paths, then I have to get a list of the items in those paths, and then traverse those and filter out non-directories from that in order to get a pseudo-list of steam logins on a users computer. (Basically just getting a list of any existing directories (directories only!) under either of those 2 paths)

Is there a shorter way of doing this (Other than condensing for loops into single lines?)?

I would much rather be given an english-worded solution so I can put it together myself; rather than code. It’s the only way I’ll truly learn the proper way. Even a nice little hint or excerpt so I can figure it out on my own would be nice.

And: Do lists in for loops always have to be traversed like:

for x in range(len(somelist)):

or is there something shorter than using range(len( ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T12:11:25+00:00Added an answer on May 13, 2026 at 12:11 pm
    for i in range(len(somelist)):
       something( somelist[i] )
    

    should be written as

    for x in somelist: 
        something( x )
    

    Also you can write everything much shorter:

    def GetSteamAccts():
        Steamapp_Folders = [f for f in ("C:\\Program Files (x86)\\Steam\\steamapps\\", 
                                        "C:\\Program Files\\Steam\\steamapps\\") 
                              if os.path.isdir(f)]
        return [os.path.join(root, folder) 
                        for root in Steamapp_Folders 
                        for folder in os.listdir(root) 
                        if os.path.isdir( os.path.join(root, folder)) ]
    

    This looks cleaner and actually does what you wanted: 😉

    def subfoldernames( root ):
        for folder in os.listdir(root):
            path = os.path.join(root, folder)
            if os.path.isdir(path):
                yield folder # just the name, not the path
    
    # same, just shorter:
    def subfoldernames( root ):
        # this returns a generator, written as a generator expression
        return ( folder for folder in os.listdir( root ) 
                        if os.path.isdir(os.path.join( root, folder )) )
    
    def GetSteamAccts():
            Steamapp_Folders = ("C:\\Program Files (x86)\\Steam\\steamapps\\", 
                                                 "C:\\Program Files\\Steam\\steamapps\\")
            for folder in Steamapp_Folders:
                if os.path.isdir(folder):
                    # only the subfolders of the first path that exists are returned
                    return list(subfoldernames( folder )) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code: def addcar @car = Car.new(params[:car]) render :action => 'list' end
I have this snippet of Scala code: def prologList(l: List[ScalaObject], sep: String) = [
I have this code: def setVelocity (x, y, yaw) setVelocity (Command2d.new(x,y,yaw)) end def setVelocity
I have this code: def display(self): print self.doc.toprettyxml(indent= ) strigName ='/Users/my_user/Desktop/python/' + str(datetime.datetime.now()) +
I have this code: class DocumentIdentifier attr_reader :folder, :name def initialize( folder, name )
So I have this code inside my lib/ folder: class GlobalConfig::SetHelper def self.yes_no_input(configuration) value
I have this simple code in my user_controller.rb file #listing all users def index
I have this in my application_helper.rb : def bbcode(text) # Code snippets text.gsub!(/\[code=?[']?(.*?)[']?\](.*?)\[\/code\]/mis) {
I have code like this (simplified): def outer(): ctr = 0 def inner(): ctr
If I have a block of code like this: def show @post = Post.find(params[:id])

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.