-2

I am trying to locate/edit a file within AppData. This is what I am doing:

 if os.path.isfile(getenv("APPDATA") + "File"):
        print("Found file!")

I am then going to open that file

conn = sqlite3.connect(getenv("APPDATA") + "File")

However, it just creates a temp file called RoamingFile there. Eventhough my file is in the AppData directory. I have played around with permissions with no luck. I can also access it if I go straight to the path eg C:\Users\user\AppData.

I have hit a brick wall really because I have been trying to solve it/look for answers for hours. - Thanks

Bitten Fleax
  • 262
  • 4
  • 20

2 Answers2

5

You've missed a "\\"... and you should be using os.path.join:

filepath = os.path.join(os.getenv("APPDATA"), "File")
if os.path.isfile(filepath):
    # do something
dhke
  • 15,008
  • 2
  • 39
  • 56
ComputerFellow
  • 11,710
  • 12
  • 50
  • 61
  • Wow, such a small mistake. However, I still cannot open the sqlite file? Well, I can it just cannot find a table in it which I am 100% sure there is because I am viewing it. – Bitten Fleax Dec 12 '15 at 14:34
  • @BittenFleax Are you viewing the correct file? Because from your question you seem to view a file in `C:\User\username\AppData\File`, but make a connection to `C:\User\username\AppData\Roaming\File`. – dhke Dec 12 '15 at 14:38
  • Sorry, I accidentally editor your answer instead of mine. Rolled back. And time to stop working :) – dhke Dec 12 '15 at 14:51
  • @dhke Christmas is almost here :-) – ComputerFellow Dec 12 '15 at 14:53
4

You are missing the path separator.

The %APPDATA% variable usually points to r'C:\User\username\AppData\Roaming'. When you append 'File' to this you get r'C:\User\username\AppData\RoamingFile'

Use os.path.join() to join paths:

filename = os.path.join(getenv("APPDATA"), "File")
if os.path.isfile(filename):
    print("Found file!")

Note that if you want to access ...\AppData\Local, it is probably best to use the Win32 API function to retrieve its location. This either needs ctypes or e.g. the pywin32 module as outlined in this answer.

from win32com.shell import shell, shellcon
app_data_local = shell.SHGetFolderPath(0, shellcon.CSIDL_LOCAL_APPDATA, None, 0)
dhke
  • 15,008
  • 2
  • 39
  • 56
  • Hello @dhke, I've added this answer a while back too, could you tell me how I could address my answer better, since this looks like the same answer that I've given :-) – ComputerFellow Dec 12 '15 at 14:37
  • Ohhhhh right. I misunderstood the way it works completely. Thanks. If it goes to Roaming. How would you locate to AppData/Local then? – Bitten Fleax Dec 12 '15 at 14:38
  • @ComputerFellow Classical case of concurrent edit. I wrote mine while you where still editing yours. – dhke Dec 12 '15 at 14:39
  • @dhke +1 haha, yup, feels a bit odd! :-) – ComputerFellow Dec 12 '15 at 14:39
  • @BittenFleax By asking Windows to hand it to you. Which requires an additional library or some fiddling around with ctypes, however. – dhke Dec 12 '15 at 14:50
  • @dhke Ah so it is difficult then? – Bitten Fleax Dec 12 '15 at 14:55
  • @BittenFleax That depends on your notion of difficult. Windows has an API call for it, but that isn't exposed to Python code by any of the builtin modules. Hence `pywin32`. – dhke Dec 12 '15 at 15:40
  • @dhke Ah right okay. I did your method with win32com and worked fine. I had to to some tweaking with setup.py for py2exe to compile it correctly. But all works now! Thanks – Bitten Fleax Dec 12 '15 at 16:42