1

I'm writing several related python programs that need to access the same file however, this file will be updated/replaced intermittently and I need them all to access the new file. My current idea is to have a specific folder where the latest file is placed whenever it needs to be replaced and was curious how I could have python select whatever text file is in the folder.

Or, would I be better off creating a program that has a Class entirely dedicated to holding the information of the file and have each program reference the file in that class. I could have the Class use tkinter.filedialog to select a new file whenever necessary and perhaps have a text file that has the path or name to the file that I need to access and have the other programs reference that.

Edit: I don't need to write to the file at all just read from it. However, I would like to have it so that I do not need to manually update the path to the file every time I run the program or update the file path.

Edit2: Changed title to suit the question more

  • This is probably a duplicate of https://stackoverflow.com/questions/3207219/how-do-i-list-all-files-of-a-directory. – r0the Nov 29 '17 at 22:24
  • What kind of data are you trying to store in the file? – randyr Nov 29 '17 at 22:26
  • @r0the not entirely, it's more so that I want all my programs I need to run to be able to access the same file without manually selecting it every single time, only when it updates. And for ease of use, I would like to not have to modify the code every time I need to access the file. – Jordan Murray Nov 29 '17 at 22:33
  • @Randyr The file is a large collection of companies and information about them, each line being a different company. There are certain fields of information I want to access from different programs and they need to run each other from time to time. For instance, when updating a company to be affiliated, I need to run my one program again that moves the information to an excel file and formats it to have a green background to show it is an affiliated company. I want to run that every time after my addAffiliate program without having to constantly select the file. – Jordan Murray Nov 29 '17 at 22:43
  • @JordanMurray Would a database such as sqlite perhaps be a better choice for such structured data? – randyr Nov 29 '17 at 22:56
  • @Randyr I have thought about using sqlite however, since the program will mostly be used by individuals with no sql experience but have experience with Excel and so I'm trying to tailor it towards that. accessing the data itself is not quite the problem, as of right now everything works _if_ I give it the exact paths to the file in the code but this will be used for years to come (where I will not be involved) and needs to be easy to use for individuals with almost no coding experience of any sort. – Jordan Murray Nov 29 '17 at 23:01
  • do you just want to open the most recently modified file in a pre-defined directory? - https://docs.python.org/3/library/os.html#os.stat – Marcin Nov 30 '17 at 05:42
  • @Marcin in a sense yes. – Jordan Murray Dec 01 '17 at 04:38

3 Answers3

0

If the requirement is to get the most recently modified file in a specific directory:

import os

mypath = r'C:\path\to\wherever'

myfiles = [(f,os.stat(os.path.join(mypath,f)).st_mtime) for f in os.listdir(mypath)]

mysortedfiles = sorted(myfiles,key=lambda x: x[1],reverse=True)

print('Most recently updated: %s'%mysortedfiles[0][0])

Basically, get a list of files in the directory, together with their modified time as a list of tuples, sort on modified date, then get the one you want.

Marcin
  • 1,889
  • 2
  • 16
  • 20
-1

It sounds like you're looking for a singleton pattern, which is a neat way of hiding a lot of logic into an 'only one instance' object. This means the logic for identifying, retrieving, and delivering the file is all in one place, and your programs interact with it by saying 'give me the one instance of that thing'. If you need to alter how it identifies, retrieves, or delivers what that one thing is, you can keep that hidden.

It's worth noting that the singleton pattern can be considered an antipattern as it's a form of global state, it depends on the context of the program if this is a deal breaker or not.

Zooby
  • 325
  • 1
  • 7
  • Keep in mind that the singleton pattern alone doesn't address the underlying problem you asked about: "having several related Python programs access the same file". – payne Nov 29 '17 at 22:36
  • True, @payne, I'm wondering If by making a main controller program that accesses all of them and get the file originally from that, if I can save that information until I need to "set up" or provide a new file for the system. Then just access and pass the file information to the other programs from the controller program. **Edit:** This whole thing might be outside of the bounds of python but I like its ability to work with data. – Jordan Murray Nov 29 '17 at 22:38
-2

To "have python select whatever text file is in the folder", you could use the glob library to get a list of file(s) in the directory, see: https://docs.python.org/2/library/glob.html

You can also use os.listdir() to list all of the files in a directory, without matching pattern names.

Then, open() and read() whatever file or files you find in that directory.

payne
  • 13,833
  • 5
  • 42
  • 49
  • Couldn't I also use fnmatch with for file in os.listdir('.'): if fnmatch.fnmatch(file, '*.txt'): ? – Jordan Murray Nov 29 '17 at 22:28
  • The question doesn't seem to be about getting files in a folder, but more about being able to access a single file from multiple places in the code. Therefore this answer does not seem relevant to the question – randyr Nov 29 '17 at 22:35
  • @Randyr OP stated problem as: "I'm writing several related python programs that need to access the same file however, this file will be updated/replaced intermittently and I need them all to access the new file". I interpreted that as having different Python programs having to find and access the same file....? – payne Nov 29 '17 at 22:40