![]() |
|
[Python] Python text filter. How to not manually drag file? - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [Python] Python text filter. How to not manually drag file? (/showthread.php?tid=6180699) |
Python text filter. How to not manually drag file? - Beginner1234 - Nov 10, 2021 Where I have to drag the file before the filter.This is my existing code: [code]input = open("first file.txt", "rt") output = open("revised.txt", "wt") for line in input: output.write(line.replace("Bob", "James").replace("Xavier", "Jake")) input.close() output.close() Now, this works but I have to manually add "first file.txt" to python files and the program doesn't search the computers files. How would I make the program automatically search the computers files? RE: Python text filter. How to not manually drag file? - Edge - Nov 11, 2021 What exactly are you trying to do? I'm new to python but I had to create something similar. RE: Python text filter. How to not manually drag file? - 3phase - Nov 11, 2021 (Nov 10, 2021 07:26 PM)Beginner1234 Wrote: Where I have to drag the file before the filter.This is my existing code: [code]input = open("first file.txt", "rt") output = open("revised.txt", "wt") for line in input: output.write(line.replace("Bob", "James").replace("Xavier", "Jake")) input.close() output.close() Searching throughout your whole computer is a very bad idea, it's very consuming in terms of resources, and time. Also, there might be a few files named "first file.txt" as you can have the same file name in other directories, so if you still go on that implementation, you gotta think if you want to keep searching or stop after the first file is found ( wouldn't recommend that approach anyway.. ) Basically, your code shouldn't mess with files you don't know, so that drag and drop to the folder where the code is, is necessary. ( or specifying the path to the specific file ) |