Python Forum
Function not executing each file in folder
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Function not executing each file in folder
#8
Complaining that text is not defined is a valid complaint. There is no variable named "text" defined in ma() or in the global scope. There is a variable named "text" defined in run(), but that variable, like all local variables in a function, is not visible outside run().

Looking at you initial code, ma() used "data" and "vet". You got data by doing this:
    text = docx2txt.process(files[i])
    text2 = text.replace(":", " ")
    text3 = text2.replace(",", " ")
    text4 = text3.replace("_", " ")
    data = text4.split()
which is the same as:
text = docx2txt.process(f).replace(":", " ").replace(",", " ").replace("_", " ")
data = text.split()
And you got vet by doing this:
vet = ""
for j in data:
	vet += j + ", " 
which is the same as this:
", ".join(text.split())
Since ma() needs both data and vet, and vet is easily created from data, I think it makes more sense to pass data to ma() and have ma() create vet.
def ma(data):
    vet = ", ".join(data)
    ...

def run():
        for f in os.listdir(path):
                if f.endswith('.docx'):
                        text = docx2txt.process(f).replace(":", " ").replace(",", " ").replace("_", " ")
                        ma(text.split())  # This creates data from text and passes it to ma
                        print('Transfert de donnée Réussi!')
Reply


Messages In This Thread
RE: Function not executing each file in folder - by deanhystad - Aug-22-2022, 06:55 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare folder A and subfolder B and display files that are in folder A but not in su Melcu54 3 639 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  Reading a file name fron a folder on my desktop Fiona 4 1,000 Aug-23-2023, 11:11 AM
Last Post: Axel_Erfurt
  Dynamic File Name to a shared folder with open command in python sjcsvatt 9 6,211 Jan-07-2022, 04:55 PM
Last Post: bowlofred
  Code to check folder and sub folders for new file and alert fioranosnake 2 2,002 Jan-06-2022, 05:03 PM
Last Post: deanhystad
  Compare filename with folder name and copy matching files into a particular folder shantanu97 2 4,592 Dec-18-2021, 09:32 PM
Last Post: Larz60+
  How to import file and function in another folder SriRajesh 1 3,261 Dec-18-2021, 08:35 AM
Last Post: Gribouillis
  How to run an exe file in the Scripts folder using py.exe? quazirfan 2 3,010 Sep-08-2021, 01:00 AM
Last Post: quazirfan
  Move file from one folder to another folder with timestamp added end of file shantanu97 0 2,522 Mar-22-2021, 10:59 AM
Last Post: shantanu97
  executing a bash file - revisited ebolisa 7 3,002 Feb-10-2021, 08:05 PM
Last Post: Gribouillis
  Writing to file in a specific folder evapa8f 5 3,514 Nov-13-2020, 10:10 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020