I want to build a script that does the following:
The script should do make an arry from the directory where the script is with os.listdir(). Then the script has to test if the value in the array already excists in the outcome file, if not the name in the array should be written to the file alphabetically.

For example:

array=

The outcome in my file should be:
ABC
computer
hallo
myname
test123
test456
zipcode

Could anyone please help me to make a script like this?
I already started making this: But the problem with this is. That everything is written to 'films.txt' and nothing is checked if the name in lijst_films already excists in 'films.txt'

import os,sys,re

lijst_films=os.listdir()
file='films.txt'

if not os.path.exists(file):
    file=open('films.txt','w')
    file.close()

for x in lijst_films:
    print(x)
    x=re.sub('20\d\d*.*', '', x)
    x=re.sub('1080*.*', '', x)
    x=re.sub('\(' ,'' ,x)
    x=re.sub('\)' ,'' ,x)
    x=x.replace('.'or'',' ')
    x=x.replace('[isoHunt]','')
    if x[-1] == ' ':
        x=x.replace(x,x[:-1])
    if x[-0] == ' ':
        x=x.replace(x,x[1:])
    print(x)
    with open('films.txt','a') as file:
        file.write('%s\n' %(x))

Thank you in advance.

Recommended Answers

This might help ...

array = ['test123','hallo','ABC','zipcode','Myname','computer','test456']

# sort case insensitive
sorted_array = sorted(array, key=str.lower)

print(array)
print(sorted_array)

''' result -->
['test123', 'hallo', 'ABC', 'zipcode', 'Myname', 'computer', 'test456']
['ABC', 'computer', 'hallo', 'Myname', 'test123', 'test456', 'zipcode']
'''
Jump to Post

All 2 Replies

This might help ...

array = ['test123','hallo','ABC','zipcode','Myname','computer','test456']

# sort case insensitive
sorted_array = sorted(array, key=str.lower)

print(array)
print(sorted_array)

''' result -->
['test123', 'hallo', 'ABC', 'zipcode', 'Myname', 'computer', 'test456']
['ABC', 'computer', 'hallo', 'Myname', 'test123', 'test456', 'zipcode']
'''

I am going to test this out!

Thanks for the reply!

Be a part of the DaniWeb community

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