I have got a file whith gz extension. Basically it was just a python file with py extension. After some manipulaition with that file It bacome file with gz ext. How to get it back?
Thanks in advance?
You did one of two things.
You compressed the file using a command like zip, winzip, gzip etc
You renamed the file with .gz as the extension
A file extension is just an indicator of the expected file contents. I can have a text file with python code and a gz extension.
To fix your problem, open the file in a text editor. If the contents look like garbage, you've opened a compressed file and will need to uncompressed it using appropriate software.
Most likely, you will find that the file contains readable python code. In that case simply save the file with the correct extension.
An pay closer attention to how you are manipulating your files in the future.
Related
I am trying to learn about I/O operations in python. I used open() function before to open text files (I used relative path). Now I am trying to do this on an absolute path but it keeps getting me FileNotFoundError even though the filename and directory is correct. I just want to learn why it is causing a problem. And how can I fix the issue ?
This is my code to open the input.txt file in a read mode.
file = open("D:/Audio/input.txt", 'r')
lines = file.readlines()
This is the screenshot of the directory where I have the files
Your screenshot shows that there is a text file with name input.txt (Note that .txt here is not extension but a part of file name).
But in your code you are trying to read from a text file input that doesn't exist. That is the reason you get FileNotFoundError.
Rename it to just input.
Also from your screenshot there are two text files - output.txt and output. When you add extension they become output.txt.txt and output.txt.
When you program, it is often useful to be able to see the full filename and the extension. Follow this tutorial to see how you can enable this functionnality.
Is there a way to work with (i.e. edit, create, delete...) files such as .docx1, and .xlsx inside a zip file without unzipping the file in Python?
There is no way to work (intelligent) with a file without reading it first. Unzipping == reading.
I have a .torrent file that contains a .bz2 file. I am sure that such a file is actually in the .torrent because I extracted the .bz2 with utorrent.
How can I do the same thing in python instead of using utorrent?
I have seen a lot of libraries for dealing with .torrent files in python but apparently none does what I need. Among my unsuccessful attempts I can mention:
import torrent_parser as tp
file_cont = tp.parse_torrent_file('RC_2015-01.bz2.torrent')
file_cont is now a dictionary and file_cont['info']['name']='RC_2015-01.bz2' but if I try to open the file, i.e.
from bz2 import BZ2File
with BZ2File(file_cont['info']['name']) as f:
what_I_want = f.read()
then the content of the dictionary is (obviously, I'd say) interpreted as a path, and I get
No such file or directory: 'RC_2015-01.bz2'
Other attempts have been even more ruinous.
A .torrent file is just a metadata file, indicating where to get the data and the filename of the file. You can't get the file contents from that file.
Only once you have successfully downloaded this torrent file to disk (using torrent software) you can then use BZ2File to open it (if it is .bz2 format).
If you want to perform the actual download with Python, the only option I found was torrent-dl which hasn't been updated for 2 years.
I have a *.tar.gz compressed file that I would like to read in with Python 2.7. The file contains multiple h5 formatted files as well as a few text files. I'm a novice with Python. Here is the code I'm trying to adapt:
`subset_path='c:\data\grant\files'
f=gzip.open(filename,'subset_full.tar.gz')
subset_data_path=os.path.join(subset_path,'f')
The first statement identifies the path to the folder with the data. The second statement tells Python to open a specific compressed file and the third statement (hopefully) executes a join of the prior two statements.
Several lines below this code I get an error when Python tries to use the 'subset_data_path' assignment.
What's going on?
The gzip module will only open a single file that has been compressed, i.e. my_file.gz. You have a tar archive of multiple files that are also compressed. This needs to be both untarred and uncompressed.
Try using the tarfile module instead, see https://docs.python.org/2/library/tarfile.html#examples
edit: To add a bit more information on what has happened, you have successfully opened the zipped tarball into a gzip file object, which will work almost the same as a standard file object. For instance you could call f.readlines() as if f was a normal file object and it would return the uncompressed lines.
However, this did not actually unpack the archive into new files in the filesystem. You did not create a subdirectory 'c:\data\grant\files\f', and so when you try to use the path subset_data_path you are looking for a directory that does not exist.
The following ought to work:
import tarfile
subset_path='c:\data\grant\files'
tar = tarfile.open("subset_full.tar.gz")
tar.extractall(subset_path)
subset_data_path=os.path.join(subset_path,'subset_full')
I am new to python, and I am trying to open a video file "This is the file.mp4" and then read the bytes from that file. I know I should be using open(filename, "rb"), however I am not clear about the following things:
In what directory is python looking for my file when I use open()? (My file is located in the downloads folder, should I move it? Where?
Is using "rb" the correct way to read the bytes from a file?
So far I tried to open the file and I get this error:
IOError: [Errno 2] No such file or directory: 'This is the file.mp4'
I know it is probably an obvious thing to do, however I have looked all over the internet and I still haven't found an answer.
Thank you in advance!
By default, Python opens the file from the current working directory, which is usually the folder where the .py script of the program is located.
If you move the video file in the same directory as the script, it should work.
You can also view the current working directory like this:
import os
print os.getcwd()
Also, instead of moving the file, you can just change "This is the file.mp4" to "C:/Users/<username>/Downloads/This is the file.mp4" if you are using Windows 7 and maybe 8. You will have to change the <username> to your computer username.
Wildcards might also work: "~/Downloads/This is the file.mp4"
Finally, what are you planning to do with the video file bytes? If you want to copy the file to somewhere else, there are modules to do that.
"rb" is a correct way to read bytes of a file.