Hello!
I have written a piece of code that creates 3 folders in the working directory, asking the user to proceed even if it may overwrite existing folders. The problem is that the more pictures there are the more time it needs to create the folders. Is there any way to solve it? You'll find the code at the end of the message.
Cheers!

Dani

overwrite = ''
while not overwrite.startswith('y'): ## 'n' case will do exit
    overwrite = raw_input('> ').lower()
    if overwrite.startswith('n'):
        raise SystemExit, 'Thank you for using WIP'
        print
        print '©Acrocephalus Soft 2010'
 
#Create a Backup directory
if not os.path.exists('Backup'):
    os.mkdir('Backup')
 
#Create a Resized directory
if not os.path.exists('Resized'):
    os.mkdir('Resized')
 
#Create a marked directory
if not os.path.exists('Watermarked'):
    os.mkdir('Watermarked')

Recommended Answers

All 5 Replies

You have timing data? Which operating system? Also lines 6 and 7 are never reached. Use multiline string.

How can I get the timing data? I am working on a Ubuntu Karmic 62bits.
Cheers!

Dani

You mentioned about more files make the creation of folders slower, lets experiment:

import sys,os
from tempfile import NamedTemporaryFile
from time import clock

if not os.path.exists('test'):
    os.mkdir('test')

dirs = ['a','b','c']

os.chdir('test')
filenum=0
for i in range(10):
    t=clock()
    for filen in range(filenum, filenum+40):
        open('Testfile%3s'% filen,'w').write('Testing\n')
    filenum+=40
        
    for directory_name in dirs:
        if os.path.exists(directory_name):
            os.rmdir(directory_name)
    print('Directories removed, files count %i. Lap time %.2f ms' %
          (len(os.listdir(os.curdir)), 1000*(clock()-t)))
    t=clock()
    for directory_name in dirs:
        os.mkdir(directory_name)
    print('%i directories created in %.2f ms' %
          (len(dirs), 1000*(clock()-t)))
[os.remove(f) for f in os.listdir(os.curdir) if os.path.isfile(f)]
    
raw_input('Ready, push Enter to finish!')
""" Test run output:
Directories removed, files count 40. Lap time 31.86 ms
3 directories created in 54.48 ms
Directories removed, files count 80. Lap time 89.94 ms
3 directories created in 1.30 ms
Directories removed, files count 120. Lap time 96.05 ms
3 directories created in 1.74 ms
Directories removed, files count 160. Lap time 78.20 ms
3 directories created in 45.21 ms
Directories removed, files count 200. Lap time 85.94 ms
3 directories created in 1.01 ms
Directories removed, files count 240. Lap time 101.36 ms
3 directories created in 0.87 ms
Directories removed, files count 280. Lap time 79.81 ms
3 directories created in 67.63 ms
Directories removed, files count 320. Lap time 336.74 ms
3 directories created in 1.66 ms
Directories removed, files count 360. Lap time 79.56 ms
3 directories created in 0.63 ms
Directories removed, files count 400. Lap time 81.56 ms
3 directories created in 20.30 ms
Ready, push Enter to finish!
"""

Which is the clock part of the script? I will test it on my machine.
Cheers!

Dani

from time import clock
t=clock()
#code to test
 timeittook=clock()-t #in seconds

There exist also timeit module, but I have not used it.

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.