How can I use numpy.savetxt to output text file in another directory?
the current working directory is /home/user/current/, but I want to save the txt file into /home/user/test/. How to modify the following code?Thanks.
out_name = "/home/user/test/txtfile.txt"
numpy.savetxt(out_name,output,fmt = '%d \r %d \r %.3f \r\n')
Assuming fmt is compatible with output, your code should save data to out_name.
Is it not working? If so, what was the full traceback error message?
Related
I am trying to extract a file name with this format--> filename.tar.gz10
I have tried mutpile wayd but for all of them, I get the error that is unknow format. it works fine for files ends with tar.gz00. I tried to change the name but still does not work.
Here are what I have tried,
import tarfile
file = tarfile.open('filename.tar.gz10')
file.extractall('./extracted_path')
file.close()
Another way is,
shutil.unpack_archive('./filename.tar.gz10', './extracted_path', 'tar.gz17')
Thanks for your help in advance.
This coule be because the archive was split into smaller chunks, on linux you could do so using the split -b command so one big file is actually multiple smaller ones now, and they are named like
file.tar.gz01
file.tar.gz02
file.tar.gz03
file.tar.gz04
etc...
you wont be able to decompress these file individually, so you have to concatenate them first into one file then decompress.
To verify whther it was split or not, run file {filename} and if does not recognize it as a gzip compressed archive then it is propably split (this is why you get unknown format error)
You can try to do the following:
from glob import glob
import os
path = '/path/to/' # location of your files
list_of_files = glob(path + '*.tar.gz*') # list all gzip files
bash_command = 'gzip -dk filename.tar.gz' + ' '.join(list_of_files) # create bash command to concatenate the files
os.system(bash_command)
According to thread:
Linux: fast creating of formatted output file (csv) from find command
there is a suggested bash command, including awk (which I don't understand):
find /mnt/sda2/ | awk 'BEGIN{FS=OFS="/"}!/.cache/ {$2=$3=""; new=sprintf("%s",$0);gsub(/^\/\/\//,"",new); printf "05;%s;/%s\n",$NF,new }' > $p1"Seagate-4TB-S2-BTRFS-1TB-Dateien-Verzeichnisse.csv"
With this command, I am able to create a csv file containing "05;file name;full path and file name" of the directory and file content of my device mounted on /mnt/sda2. Thanks again to -> tink
How must I adapt the above command to receive date(&time) and file size also?
Thank you in advance,
-Linuxfluesterer
Is there a way to append to a file and read it with the same "open" file command in Python3.7? I mean I do not want to have two open statements, one for open("//", "a") and one for open("//", "r"). What I am trying to achieve is run a script which appends the output to the file, and then read the last line of the file. "a+" does not help; it gives a index of out range for readlines()[-1].
Thanks in advance.
Opening the file in a+ makes the file pointer point to the end of the file, which makes it hard to read the last line. You can instead open the file in r+ mode, iterate over the file object until you obtain the last line, and then append the additional output to the file:
with open('file', 'r+') as file:
for line in file:
pass
file.write(output)
# variable line now holds the last line
# and the file now has the content of output at the end
I'm trying to read in a text file to work with Word Clouds. Here is the syntax I'm trying:
# Read the whole text.
text = open(r'C:\Users\mswitajski\Desktop\alice.txt').read()
But I keep getting the following error:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\mswitajski\\Desktop\\alice.txt'
I've triple checked the file name, tried reading it as a raw file, changed the slashes and everything but I continue to get the same error.
Well, if someone reaches up to here and still could not find the solution then here is the more pythonic way of doing the absolute path in windows.
Instead of using:
text = open(r'C:\Users\mswitajski\Desktop\alice.txt').read()
use os.sep, in conjunction of os.path.join like the following:
import os
text = open(os.path.join('C:', os.sep, 'Users', 'mswitajski', 'Desktop', 'alice.txt')).read()
Try changin the path to this"
'C:\Users\mswitajski\Desktop/alice.txt'
Sometimes windows won't find/recognize the file path when the file is specified like this
'C:\Users\mswitajski\Desktop\alice.txt'
In the answer it shows up as only one \ but you still need 2 like your previous path. The only difference is the last slash /. Hope that works.
At your text raw file (alice.txt) try delete the .txt.
The file probably is named alice.txt.txt
I face the same issue and solve it by deleted the .txt.
I had to use double slashes instead of one, because python interpreted it as a escape sequence. My final string was:
C:\\Users\\ArpitChinmay's\\AppData\\Roaming\\Code\\User\\globalStorage\\moocfi.test-my-
code\\tmcdata\\TMC workspace\\Exercises\\hy\\hy-data-analysis-with-python-
2020\\part02-e04_word_frequencies\\src\\alice.txt
However, it worked this way too,
C:\\Users\\Arpit Chinmay's\\AppData\\Roaming\\Code\\User\\globalStorage\\moocfi.test-
my-code\\tmcdata\\TMC workspace\\Exercises\\hy\\hy-data-analysis-with-python-
2020\\part02-e04_word_frequencies\\src/alice.txt
I want to unzip a file that contains a .dat file using SAS. I have over 100 files to unzip and therefore I want to do it automatically with SAS. I've tried to use the following:
FILENAME ZIPFILE SASZIPAM 'Z:\folder\file';
DATA newdata;
INFILE ZIPFILE(file.dat) dsd DLM='|';
INPUT var1 var2 var$;
RUN;
That doesn't work. Is there a problem when you use ZIPFILE SASCIPAM to unzip a .dat file? I have SAS 9.3.
Is there a better alternative?
I guess you could use macros to do it, but it might get a bit messy. SAS has for many years offered the X command for contacting the command prompt. This method requires that you have downloaded a free file archiver/de-compresser (e.g. 7-Zip, WinRAR etc.).
I like using the command line version of 7-Zip. I use a 32-bit machine, so you might have to use a different .exe file.
The syntax for unzipping multiple files in a directory:
data _null_;
X "cd (7-Zip_installed_location)"; <=== File that contains the 7za.exe
X "7za e (zip_files_location)*.zip -o(output_destination)";
run;
For Example, I have some zip files in the folder called "Compressed":
data _null_;
X "cd C:\7-Zip Comm";
X "7za e C:\sasdata\Compressed\*.zip -oC:\sasdata\New";
run;
e stands for "extract".
* means for all.
-o stands for "output".
I have never seen SASZIPAM as a file reference type.
I would do this like this:
filename zipfile ZIP 'Z:\folder\file.zip';
DATA newdata;
INFILE ZIPFILE(file.dat) dsd DLM='|';
INPUT var1 var2 var$;
RUN;
It's possible I am missing that type in the documentation. Can you paste the ERROR you get in your log?
Double quotes for each of the path will resovle any issue while extracting .z file on windows 7 (SAS 9.3): (FYI - 2nd statement is in one line)
data _null_;
x '"C:\Program Files\7-Zip\7z.exe" x "E:\sas\config\Lev1\SASApp\Data\*.Z" -o"E:\sas\config\Lev1\SASApp\Data"';
run;
Regards
Bhavin