-1

I have a folder that contains many sub folders. First I want to check if there is any ".txt" file. If yes I will do nothing regardless of any other file formats that exist in the same folder. If there is not, I will check whether there is ".csv", If yes I want to convert it/them to ".txt". Same also applies here, if I find ".csv", I don't care about other file formats

If there is no ".txt" nor ".csv", probably it is ".doc". So, I want to convert it\them into ".txt"

So the logic is: Find ".txt", if you find; continue and don't do anything with that folder. If you don't find ".txt", look for ".csv", if you find ".csv", convert it\them to ".txt". Otherwise, look for ".doc" and do the same. How I could do that ?

Thierry Lathuille
  • 23,663
  • 10
  • 44
  • 50
Cenk_Mitir
  • 113
  • 11
  • Maybe try to reformat your problem from one big chunk of unstructured text to more digestible form. :) – custom_user Jun 23 '17 at 10:23
  • 1
    Is it better now :) ? – Cenk_Mitir Jun 23 '17 at 10:33
  • 1
    You can search the subfolders with `glob.glob('**/*.txt', recursive=True)` with Python version >= 3.5, for older versions the recursive search via `**` works with the package `glob2` whch is available via `pip install glob2`. – Michael H. Jun 23 '17 at 10:54
  • For doc to txt conversion you can see https://stackoverflow.com/questions/125222/extracting-text-from-ms-word-files-in-python Csv to txt is rather straightforward. You just change extension since both are plain types. – custom_user Jun 23 '17 at 10:59

1 Answers1

1

First you would need to find any directories in your desired folder. You could use os.walk(directory) which returns a generator of the folder and any sub-folders, as well as the files in it. Then you simply need to write some if / elif statements for what you want. Here's the first part of what you need.

from os import walk, rename, join


directory = './'
for folder, subfolders, filenames in os.walk(directory):
    if any([filename.endswith('.txt') for filename in filenames]):
        continue  # goes to the next iteration
    elif any([filename.endswith('.csv') for filename in filenames]):
        for filename in filenames:
            if filename.endswith('.csv'):
                rename(join(folder, filename), join(folder, filename.replace('.txt')))  # rename changes a file's name, and join here is used for putting together the full path of the file

You still need to handle the doc files and I would suggest you to always make sure you understand what your code is doing. If you're not familiar with iteration or loops in general, check this. Note that the tutorial is for Python 2, apart from the parentheses in print, everything else stays the same.

Stam Kaly
  • 668
  • 1
  • 11
  • 26
  • So can I add another else if for the word documents ? – Cenk_Mitir Jun 23 '17 at 12:04
  • Can I use if file name ends with something else instead of csv ? how it would be in this case? – Cenk_Mitir Jun 23 '17 at 12:12
  • Yes, just replace the `.csv` with `.doc` in another `elif`. If you want to do exactly the same for both file extensions as you mentioned – Stam Kaly Jun 23 '17 at 12:15
  • and I want to create new files in the same directory I should add chdir to somewhere ? – Cenk_Mitir Jun 23 '17 at 12:17
  • My code doesn't change the working directory, just create the files to the directory you want, changing the directory is only necessary when you're doing a lot of tasks on that particular one. – Stam Kaly Jun 23 '17 at 12:20
  • It would have been really helpful if I could put new files to the folders that they have originated from – Cenk_Mitir Jun 23 '17 at 12:22
  • Because later on I will also merge them – Cenk_Mitir Jun 23 '17 at 12:22
  • The folder of each file is named under the variable `folder` under each `if` / `elif`. You can use that name to create any file. Still, did you read the tutorial I told you to? If not, you better do, I won't always be here to solve your problems. – Stam Kaly Jun 23 '17 at 12:26
  • I read it but tutorial that you send me is about if / else I asked you a question about changing the directory, but anyways, thank you – Cenk_Mitir Jun 23 '17 at 12:30