hi guys n girls
im making a list of the biggest files on my computer i have got most of the coding done but i have hit a brick wall with the sort function.
i can sort the size or the file name but as they are both in the same list i cannot sort by size as they have got rid of this function in python 3.1 if anyone can help me with this it would be great!
\/ example of list \/
size F.name
[256, '.\\abc.py']
[366, '.\\test123.py']
[286, '.\\testab.py']
[0, '.\\dir1\\Copy (10) of New Text Document (2).txt']
[0, '.\\dir1\\Copy (10) of New Text Document.txt']
[82, '.\\dir1\\Copy (10) of tessdfsdt5.txt']
\/ code to get the list \/

import os
folder = ('.')
fsize=list()
filename=list()
filesize=list()
for (path, dirs, files) in os.walk(folder):
  for dirs in path:
    for file in files:
      filename = os.path.join(path, file)
      file_size = os.path.getsize(filename)
      fsize= file_size, filename
      Fsize=list(fsize)
      #Fsize.sort()--->>> TypeError: unorderable types: str() < int()
      print(Fsize)
    break

Recommended Answers

All 5 Replies

I think there is something wrong in your logic in the first place.
Do you intend the sort the files by size?
Your code is currently trying to sort a list of 2 elements containing just
the file size and filename.

hey sorry im still a newbie to python lang.
i have created the list containing the file and date but cannot sort it
and yes i do want to sort them by the size. i have done this in 2.5 but as i have seen from forums you cannot sort a list of two different elements.
do you know why they got rid of this feature as i have used it for quite a few little tasks in 2.5 and always worked perfectly.
sorry if this is confusing, I've confused myself at some points.

Depending on how big of a list this might be, you could use a pysqlite3 database. I included an example below.

import sqlite3

conn = sqlite3.connect("PATH\DATABASE_NAME") #Connects to the db. Creates db if none exists with that name
c = conn.cursor()

c.execute("""create table myfiles (FileSize int, FileName text)""") #Defines table, the columns, and their types
c.execute("""insert into myfiles values (83, "C:\My Documents\My File")""") #Inserts an entry into the database
conn.commit() #Saves the changes

c.execute("select * from myfiles order by FileSize") #Defines what to select from the database, saying that the entries should be ordered in descending value (by default) from the FileSize column

for row in c:
     print row #This part takes the selection you defined above and prints it all

That should work. But that's not really necessary unless you will be working with a lot of entries. It is a handy way to organize things though. Hope that helps.
Cheers!

hey i have just tried it and it doesn't seem to want to create the database.

if possible i want to just sort the files via the size this is the code i have so far which works but does not sort properly \/

import os
folder = ('.')
#size, count = '', 0
filename=list()
file_size=list()
for (path, dirs, files) in os.walk(folder):
  for dirs in path:
    for file in files:
      #size, count = '', 0
      filename = os.path.join(path, file)
      file_size = str(os.path.getsize(filename))
      #if len(file_size) > 12:
         #'%2d' % filesize
         #count += 1
      fsize=list()
      fsize.append(file_size + filename)
      #fsize.append(filename)
      #print(fsize)
      fsize.sort()# i do not receive errors but it does not sort them or if it does i can see what it is sorting it by
      print(fsize)
      
    break

Hmm. Try this:

import sqlite3

conn = sqlite3.connect("Data") #Connects to the db. Creates db if none exists with that name
c = conn.cursor()

c.execute("""create table myfiles (FileSize int, FileName text)""") #Defines table, the columns, and their types
a = "string containing path to file (e.g. C:\My Documents\MyFile)"
c.execute("""insert into myfiles values (83, ?)""", a) #Inserts an entry into the database
conn.commit() #Saves the changes

c.execute("select * from myfiles order by FileSize") #Defines what to select from the database, saying that the entries should be ordered in descending value (by default) from the FileSize column

for row in c:
     print row #This part takes the selection you defined above and prints it all
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.