Ciarán
Ciarán

Reputation: 3

Looking to assign print output (string) to a variable

I've been struggling for the past 2 days to do something that I am sure is easy. Unfortunately I do not know python very well (nearly nothing actually) and I'm just trying to edit a script for work.

Basically it should look into a folder, get the filename and assign the filename to a variable. But everything I tried failed :

filepath = "/folder/*.*/"
for path in glob.glob(filepath):
  dirname, filename = os.path.split(path)
  print(filename)[0:-19]

var1 = filename
var2 = filename[0:-25]

I tried with

var1 = str(filename)

But nothing works. Any advice would be greatly appreciated!

Upvotes: 0

Views: 613

Answers (4)

Filip
Filip

Reputation: 128

Try this and work from there:

import os

filepath = "/"

var1 = os.listdir(filepath)
var2 = os.listdir(filepath)[2]

print var1
print
print var2

Upvotes: 0

n1c9
n1c9

Reputation: 2687

The way that your code is written now, it prints however many file names for the number of files in your folder, discards the filename variable and runs again on a new path. When you have a for loop, the variables are discarded on every iteration to make way for the new ones. So this code:

filepath = "/folder/*.*/"

for path in glob.glob(filepath):
    dirname, filename = os.path.split(path)
    print( filename )[0:-19]

Var1 = filename
Var2 = filename[0:-25]

is running for each path you supply it with initially, then keeps only the last filename when you refer back to it in the vars. I think what you want is something like this:

for path in filepath:
  dirname, filename = os.path.split(path)
  print(filename[0:-19])
  var1 = filename
  var2 = filename[0:-25]

From there, you could do anything you want with var1 and var2.

Upvotes: 0

jDo
jDo

Reputation: 4010

It might be a simple indentation issue. Currently, var1 and var2 are outside the for loop. I didn't study your script or its output thoroughly but doing what I've done below will assign the value of filename to var1 and assign a section of filename (from the beginning to the character 25 places from the end) to var2.

filepath = "/folder/*.*/"
for path in glob.glob(filepath):
  dirname, filename = os.path.split(path)
  print(filename)[0:-19]
  var1 = filename
  var2 = filename[0:-25]
  print(var1)
  print(var2)

Upvotes: 0

Jack Geller
Jack Geller

Reputation: 211

def GetFileList(FindPath,FlagStr=[]):  
    ''''' 
    #>>>FlagStr=['F','EMS','txt'] # Characters need to include
    #>>>FileList=GetFileList(FindPath,FlagStr) # 
    '''  
    import os  
    FileList=[]  
    FileNames=os.listdir(FindPath)  
    if (len(FileNames)>0):  
        for fn in FileNames:  
            if (len(FlagStr)>0):  
                #return Specified filename  
                if (IsSubString(FlagStr,fn)):  
                    fullfilename=os.path.join(FindPath,fn)  
                    FileList.append(fullfilename)  
            else:  
                #return all filename  
                fullfilename=os.path.join(FindPath,fn)  
                FileList.append(fullfilename)  

     #sort filename
     if (len(FileList)>0):  
         FileList.sort()  

     return FileList  

Upvotes: 1

Related Questions