Python Forum

Full Version: Re writing poste to make sense please help me
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hey guys I can get this to run on android but I can get it to run on Windows when I change the path can u guys help please. I do need and explanation so I can learn though
Cheers
Nathan
...........................
import os
import shutil

#change the pathway were to sort
path ="/internalsharedstorage/downloads/"
name = os.listdir(path)
folder_name = ['image','text','exe','mp3','mp4','apk','pdf','zip','rar','sav']
for x in range(0,10):
    if not os.path.exists(path+folder_name[x]):
        os.makedirs(path+folder_name[x])
# movement of files 
for files in name:
    if '.pdf' in files and not os.path.exists(path+'pdf/'+files):
        shutil.move(path+files, path+'pdf/'+files)
        
    elif '.zip' in files and not os.path.exists(path+'zip/'+files):
        shutil.move(path+files, path+'zip/'+files) 
        
    elif '.exe' in files and not os.path.exists(path+'exe/'+files):
        shutil.move(path+files, path+'exe/'+files)

    elif '.jpeg' in files and not os.path.exists(path+'image/'+files):
        shutil.move(path+files, path+'image/'+files)
        
    elif '.txt' in files and not os.path.exists(path+'text/'+files):
        shutil.move(path+files, path+'text/'+files)
        
    elif '.mp3' in files and not os.path.exists(path+'music/'+files):
        shutil.move(path+files, path+'music/'+files)
        
    elif '.sav' in files and not os.path.exists(path+'save/'+files):
        shutil.move(path+files, path+'sav/'+files)
        
    elif '.rar' in files and not os.path.exists(path+'rar/'+files):
        shutil.move(path+files, path+'rar/'+files)
        
    elif '.mp4' in files and not os.path.exists(path+'mp4/'+files):
        shutil.move(path+files, path+'mp4/'+files)

    elif '.apk' in files and not os.path.exists(path+'apk/'+files):
        shutil.move(path+files, path+'apk/'+files)
else:        
    print ('cant be moved')

Sorry it didnt come up as sorce i incased it wrong
I am writing a program to use on not android and windows in order to reorganize files into folders by there extension and delete empty folders.

At the moment it runs for android and works great but does not work for windows.

I'm fairly new to python and would very much like an explanation as to why it doesn't work and how I can change it.

Thank you for reading this so far.
Here is my code so far
import os
import shutil

#change the pathway were to sort
path ="/internalsharedstorage/downloads/"
name = os.listdir(path)
folder_name = ['image','text','exe','mp3','mp4','apk','pdf','zip','rar','sav']
for x in range(0,10):
    if not os.path.exists(path+folder_name[x]):
        os.makedirs(path+folder_name[x])
# movement of files 
for files in name:
    if '.pdf' in files and not os.path.exists(path+'pdf/'+files):
        shutil.move(path+files, path+'pdf/'+files)
        
    elif '.zip' in files and not os.path.exists(path+'zip/'+files):
        shutil.move(path+files, path+'zip/'+files) 
        
    elif '.exe' in files and not os.path.exists(path+'exe/'+files):
        shutil.move(path+files, path+'exe/'+files)

    elif '.jpeg' in files and not os.path.exists(path+'image/'+files):
        shutil.move(path+files, path+'image/'+files)
        
    elif '.txt' in files and not os.path.exists(path+'text/'+files):
        shutil.move(path+files, path+'text/'+files)
        
    elif '.mp3' in files and not os.path.exists(path+'music/'+files):
        shutil.move(path+files, path+'music/'+files)
        
    elif '.sav' in files and not os.path.exists(path+'save/'+files):
        shutil.move(path+files, path+'sav/'+files)
        
    elif '.rar' in files and not os.path.exists(path+'rar/'+files):
        shutil.move(path+files, path+'rar/'+files)
        
    elif '.mp4' in files and not os.path.exists(path+'mp4/'+files):
        shutil.move(path+files, path+'mp4/'+files)

    elif '.apk' in files and not os.path.exists(path+'apk/'+files):
        shutil.move(path+files, path+'apk/'+files)
else:        
    print ('cant be moved')
Cheers guys
Quote:but I can get it to run on Windows when I change the path
what exactly doesn't run?
What are the error messages (post verbatim plese)
I can't get it to run on Windows sorry auto correct it finds the first item to sort then tells me the directory doesnt exsist
In Windows you have to have drive letter in path.
Here a quick test i have also rewritten code using 3.6 and f-string.
import os
import shutil

path = "E:/1py_div/foo/"
file_name = os.listdir(path)
folder_name = ['image', 'text']

for file_type in folder_name:   
    if not os.path.exists(f'{path}{file_type}'):
        os.makedirs(f'{path}{file_type}')

for files in file_name:
    if '.txt' in files and not os.path.exists(f'{path}text/{files}'):
        shutil.move(f'{path}{files}', f'{path}text/{files}')
    elif '.jpg' in files and not os.path.exists(f'{path}image/{files}'):
        shutil.move(f'{path}{files}', f'{path}image/{files}')
Many thanks for the reply btw. I have just ran it on my phone so that's my phone dir normally i replace it as c:/user/near/downloads then it will read along the lines of can't find c/user/near/download/songname.mp3
Also is f-string like print

Cheers for your patience
(Apr-01-2018, 11:15 AM)Nearrivers Wrote: [ -> ]Also is f-string like print
Not like print which is a function,f-string is string formatting with super power Wink
The history of string formatting.
city = 'Oslo'
description = 'Windy'
temperature = 25

# Old way don't use it
print('%s is currently %s and temperature is %dC' % (city, description, temperature))

# Python 2.6(10-year ago) we get .format()
print('{0} is currently {1} and temperature is {2}C'.format(city, description, temperature))

# Python 3.6 we get f-string
print(f'{city} is currently {description} and temperature is {temperature}C')
All output the same:
Output:
Oslo is currently Windy and temperature is 25C
Some super power.
>>> # f-strings support any Python expressions inside the curly braces
>>> name = 'f-string'
>>> print(f"My cool string formatting is called {name.upper():*^20}")
My cool string formatting is called ******F-STRING******

>>> cost = 99.75999
>>> finance = 50000
>>> print(f'Toltal cost {cost + finance:.2f}')
Toltal cost 50099.76

>>> for word in 'f-strings are cool'.split():
...     print(f'{word.upper():~^20}')
...     
~~~~~F-STRINGS~~~~~~
~~~~~~~~ARE~~~~~~~~~
~~~~~~~~COOL~~~~~~~~
So snipsat what would be the issue with my code if I have changed the path to the one in my previous post :)
(Apr-01-2018, 07:26 AM)Nearrivers Wrote: [ -> ]then it will read along the lines of can't find c/user/near/download/songname.mp3
If that's correct,like you have copied error message.
Then is missing colon c:/user/near/download/songname.mp3
Pages: 1 2