1

I have a text file with a couple hundred file paths to text files which I would like to open, write / cut up pieces from it and save under a new name.

I've been Googling how to do this and found the module glob, but I can't figure out exactly how to use this.

Could you guys point me in the right direction?

2
  • Try here: docs.python.org/2/library/glob.html Commented Dec 2, 2015 at 13:43
  • Thanks. I saw this page, of course, but it's not so straightforward to me how I can use it. Commented Dec 2, 2015 at 13:49

2 Answers 2

1

If you have specific paths to files, you won't need to glob module. The glob module is useful when you want to use path like /user/home/someone/pictures/*.jpg. From what I understand you have a file with normal paths.

You can use this code as a start:

with open('file_with_paths', 'r') as paths_list:
    for file_path in paths_list:
        with open(file_path, 'r') as file:
            # Do what you want with one of the files here.
Sign up to request clarification or add additional context in comments.

5 Comments

This is great, very clear! Thanks! But for reason python can't fnd the files: 'No such file or directory'. Do you think these file paths are in the right format when the first file (P10088) is located in my working directory? P10088/10088-01-07-02/pca/SE1_5a0e110-5a0e6a1.evt
It all depends what is your current working directory. If the paths in your file_with_paths are relative to the directory in which you run your python script.py than it should be all right. You can check your current working directory with os.getcwd() and you can change it with os.chdir(). Check if those directories are what you expect.
The open function, if given a non-absolute path to files tries to open the file by combining result of os.getcwd() with what you give it as an argument. That's why it's important to make sure that os.getcwd() + '/' + file_path makes sense.
I think it make sense, except that the error message prints an \n after the file path, probably because there are enters after each file path. Does that matter at all?
@user90465 find out by passing file_path.strip() to the open function. strip() removes whitespace from the beginning and end of the string, so it'll get rid of the \n.
1

You can just traverse the file line by line and then take out what you want from that name. Later save/create it . Below sample code might help

with open('file_name') as f:
    for file_path in f:
        import os
        file_name = os.path.basename(file_path)
        absolute path = os.path.dirname(file_path)
        # change whatever you want to with above two and save the file
        # os.makedirs to create directry
        # os.open() in write mode to create the file

Let me know if it helps you

1 Comment

Thank you for this! Could you maybe exlplain a bit more about it? It's not very obvious to me what is what, and how to traverse over the files and change them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.