0

I first noticed this problem a bit ago when I posted this thread. Essentially, I'm getting a very strange issue where Python "sees" files that don't actually exist in my file browser when working with files.

As in, if I open a file with an absolute path, modify and print the contents of it through Python, it will print exactly what it's supposed to. But when I try to open the same file through the same absolute path on my Windows file browser, the file is not updated with my modifications.

I experienced a similar issue recently as well - renaming files. Here's a simple script I wrote to replace all spaces in filenames with underscores.

rename.py:

import os

i = 0
path = "E:/sample_path/"

for filename in os.listdir(path):
    src = path + filename
    dst = path + filename.replace(" ", "_")

    os.rename(src, dst)
    i += 1

print(str(i) + " files processed.")

Upon doing some prints in Python, I can see that all the files in the directory are being renamed correctly but it just wasn't correctly updating when I actually viewed the directory. Both in the file browser and in using the dir command. Same with creating new files in Python, they exist in the eyes of Python, but they are nowhere to be found in Windows, even with hidden files being visible and all.

Now for the interesting part: This script works if I open the python editor in cmd and import it. So I know it's all correct, no syntax errors or anything - it's just a strange error occurring with Python.

For example, if I go into command prompt and type python rename.py it won't return any errors and will even output the correct results - x files processed. but it will not actually modify any "real" files.

But if I go into command prompt and type python to bring up the cmd editor, then type import rename it gives the correct output and updates all the files correctly. So for the time being this workaround helps, but it's a very strange issue and I have yet to see anyone else encounter it. It's almost like Python creates a temporary duplicate of the filesystem and is not completing it's sync back to Windows.

I've tried reinstalling Python several times, both x64 and x86 verisons and nothing has fixed it so far.

EDIT: Here's a simplified example illustrating my issue.

write.py:

f = open("newfile.txt", "w+")
f.write("hello new file")
f.close()

read.py:

f = open("newfile.txt", "r")

for l in f.readlines():
   print(l)

If I run write.py, no errors are returned. But also no file named newfile.txt is visible in my current working directory. However, if I run read.py it prints hello new file. So clearly both of these are accessing the same invisible file somewhere.

enter image description here

ryan.kom
  • 167
  • 1
  • 15
  • If you are writing to areas you aren't allowed your write may be virtualized. https://learn.microsoft.com/en-us/windows/security/threat-protection/security-policy-settings/user-account-control-virtualize-file-and-registry-write-failures-to-per-user-locations – Noodles Apr 24 '19 at 20:03
  • @Noodles thanks for the suggestion. However, I've tried this in several locations that should be writeable (separate HDD, Desktop, etc.) and it hasn't worked. Not to mention, I assume I would face this problem using my workaround method, wouldn't I? This problem strangely seems unique to using `python file.py` to execute a script. – ryan.kom Apr 24 '19 at 20:06
  • It would be nice if you'll show us the incorrect results and describe why they're incorrect. Have you tried to debug your script step-on-step and check what's happening? Monitor it with a Process Monitor? – montonero Apr 25 '19 at 07:22
  • @montonero there's not much to show, because they don't show up anywhere. For example if I write to a file with Python, it will "successfully write" but not show up in my file browser. When I read from the same file, Python spits out the correct data, but it's still nowhere to be seen in my file browser. However, if I open the Python console and type `import scriptname` it correctly writes to a visible file I can see in my file browser. I can try to get some screenshots of the Python output to demonstrate my point, though. – ryan.kom Apr 25 '19 at 16:36
  • I mean an example of where and which files you're writing. And since you can't find the files and they're definitely exist then using a Process Monitor to check where they're writing is the best option so far. – montonero Apr 26 '19 at 07:05
  • I believe you'll need to make sure that the working directory is the same as you're expecting it. – montonero Apr 26 '19 at 23:13

0 Answers0