0

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!

1
  • I don't get what you are trying to do with [0:-19] and [0:-25]. Without it I manage to get all filenames present in all matching folders (easily editable to save the names into variables). If it's what you want, I can put the answer. Commented Apr 7, 2016 at 14:33

4 Answers 4

1
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  
Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

That did it !Thank you very much !
Glad I could help!
0

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)

Comments

0

Try this and work from there:

import os

filepath = "/"

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

print var1
print
print var2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.