Facing a strange "phantom filesystem" issue in Python - python-3.x

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.

Related

Code creates new .txt file in one .py but not in another .py

I'm writing a code which creates a new .txt file to which I will add data later on.
For testing and debugging I wrote it in another .py file and it's working. However when i copy the code to my main file it's not working anymore but it's not giving me an error either.
culturename_raw = str("filename123")
def create_rawfile():
outfile = open(culturename_raw, "a")
outfile.write("\n" + "text that I'm adding" + "\n")
outfile.close()
print("rawfile created")
create_rawfile()
When I run this code in my test .py it's creating a file with the name, writes the text und prints "rawfile created".
When I run this code in my main .py there is no error code and even prints "rawfile created" but there is no text file created.
I'm using Python 3.8 and PyCharm Community 2020.2 if that matters.
Try changing culturename_raw = str("filename123") to culturename_raw = str("filename123.txt"). You have to have the '.txt' extension.

Executable file from Python script can't write to the file

I have a python code that write some outputs to the defined output file in text. In PyCharm, my script works without any problem but when i convert my xx.py to the .exe file by using pyinstaller, exe file can't write to the output file which is already in the same path.
To write the outputs i use this code:
fg = open('input.src')
output_file = open("output.obj", "w")
for line in fg:
**do something**
output_file = open("output.obj", "a")
output_file.write("[" + str(hex(memory_location)) + "]" + " " + output + "\n")
I also tried the xx.py without PyCharm, with console, and the script work fine.
when i convert it to the .exe file ...
It's not clear what conversion process you're referring to.
Definitely PyCharm is running a standard python interpreter as a child process.
It sounds like you may be running cython or other unusual language environment in your problem case.
You wrote:
output_file = open("output.obj", "w")
...
output_file = open("output.obj", "a")
You chose not to use the with idiom.
More importantly, you chose not to output_file.close() before re-opening.
Your combination of language interpreter + filesystem may require a close before a subsequent open.
Thanks to #mypetlion, the problem was caused by the encoding of the input file which was ANSI. It need to change into UTF-8.

The system cannot find the file specified - WinError 2

Upon looping a directory to delete txt files ONLY - a message is returned indicating The System cannot find the file specified: 'File.txt'.
I've made sure the txt files that I'm attempting to delete exist in the directory I'm looping. I've also checked my code and to make sure it can see my files by printing them in a list with the print command.
import os
fileLoc = 'c:\\temp\\files'
for files in os.listdir(fileLoc):
if files.endswith('.txt'):
os.unlink(files)
Upon initial execution, I expected to see all txt files deleted except for other non-txt files. The actual result was an error message "FileNotFoundError: [WinError 2] The system cannot find the file specified: 'File.txt'.
Not sure what I'm doing wrong, any help would be appreciated.
It isn't found because the the path you intended to unlink is relative to fileLoc. In fact with your code, the effect is to unlink the file relative to the current working directory. If there were *.txt files
in the cwd then the code would have unfortunate side-effects.
Another way to look at it:
Essentially, by analogy, in the shell what you're trying to do is equivalent to this:
# first the setup
$ mkdir foo
$ touch foo/a.txt
# now your code is equvalent to:
$ rm *.txt
# won't work as intended because it removes the *.txt files in the
# current directory. In fact the bug is also that your code would unlink
# any *.txt files in the current working directory unintentionally.
# what you intended was:
$ rm foo/*.txt
The missing piece was the path to the file in question.
I'll add some editorial: The Old Bard taught us to "when in doubt, print variables". In other words, debug it. I don't see from the OP an attempt to do that. Just a thing to keep in mind.
Anyway the new code:
Revised:
import os
fileLoc = 'c:\\temp\\files'
for file in os.listdir(fileLoc):
if file.endswith('.txt'):
os.unlink(os.path.join(fileLoc,file))
The fix: os.path.join() builds a path for you from parts. One part is the directory (path) where the file exists, aka: fileLoc. The other part is the filename, aka file.
os.path.join() makes a whole valid path from them using whatever OS directory separator is appropriate for your platform.
Also, might want to glance through:
https://docs.python.org/2/library/os.path.html

python - automatically launch a text file created in the program

I did the search but i couldn't find any help, apologies if i my question is duplicate.
i am writing the code with python 3.6 and in windows environment.in my code, i opened a text file, write the data and close the file.
self.fileName = 'file path'
self.log_file = open(self.fileName, 'w')
self.log_file.write('Write results')
self.lof_file.close()
Instead of the user goes to file path and click to open it, i want to launch the file automatically after python save it.
how do i do that? please help
EDIT:
os.startfile(filepath=self.fileName)
command is working fine, but its opening with default program which is Notepad, how to open the file with specific program, for example, Notepad++
If you know the command line way of doing it, you can use the os module as follows:
import os
self.file = 'file path'
self.log_file = open(self.fileName, 'w')
self.log_file.write('Write results')
self.lof_file.close()
os.system('gedit <file_path>') # for ubuntu, gedit is generally present
For Windows, you can use:
import os
os.startfile('C:\\Users\\RandomUser\\Documents\\test.txt')
Check this answer for more details: https://stackoverflow.com/a/15055133/9332801

Python: "FileNotFoundError" Despite being able to print such files

I'm working on a Python3 script where the code walks through directories and sub-directories to pull out all gzipped warc files.
I'd like to also add that the files are not in my home directory
file_path = os.path.join('/nappa7/pip73/Service')
walk_file(parallel_bulk, file_path)
Perhaps python is not looking where i think it's looking, nevertheless, here is my walk_file functions:
def walk_file(bulk, file_path):
warc = warcat.model.WARC()
try:
for (file_path,dirs,files) in os.walk(file_path):
for filenames in files:
if filenames.endswith('.warc.gz'):
warc.load(filenames)
except ValueError:
pass
When I replace the warc.load(filenames) with a print statement like so:
if filenames.endswith('.warc.gz'):
print(filenames)
The filenames are printed out onto the console as expected. Therefore, It leads me to believe that python was able to succesfully locate all warc.gz files. However, when i try the warc.load(filenames), i get:
FileNotFoundError: [Errno 2] No such file or directory: 'Sample.warc.gz'
I can certainly use some guidance.
Thank you.
So for anyone else who has a similar issue:
changing the code to this worked:
warc.load(os.path.join(file_path, filenames))
You need to use os.path.join(file_path, filenames) instead of just filenames.
Otherwise the operating system will look for the file in the current directory instead of file_path.
(And why is filenames plural when it refers to a single filename?)

Resources