How to use 7z from the python scripts - python

I have following syntax in my python script.
x = Popen("\"C:/Program Files/7-zip/7z\" a -tzip " + new_file + " general/*")
Can anyone please explain the meaning of this syntax?
From some forums, I am explaining my understanding.
First the 7z application will be started from the command line interface from the mentioned path. Then the folder " general" in the current directory will be copied into new_file. Am I right?
Please note, I am totally new to Python. Hence please excuse my understanding.
When I run this command, I am getting following Warning. Can anyone please let me know the issue in the syntax?
7-Zip 17.01 beta (x64) : Copyright (c) 1999-2017 Igor Pavlov : 2017-08-28
Open archive: <new_file>.zip
Path = <new_file>.zip
Type = zip
Physical Size = 93678166
Scanning the drive:
WARNING: The system cannot find the file specified.
general
Writing the path like this: "<path>" is valid? What is the difference between "" and "<path>"? Frankly speaking, I have copied this from some forum for my application.
What does " general/* " means? I know that if file name to be written with the space, it should be quoted with "". But what does it mean with wildcard characters " general/*"?
Please note, this scripts it going to be executed with Windows command line interface.
Thanks in advance.

When I run this command, I am getting following Warning. Can anyone please let me know the issue in the syntax?
WARNING: The system cannot find the file specified.
general
The reason for this warning is not in the syntax; it's because there is no folder named general.
Writing the path like this: "<path>" is valid?
It seems so, but you probably don't really want to have the file name contain less-than or greater-than characters (angle brackets). Presumably where you've copied this from there's a line
new_file = "<new_file>"
- you were supposed to replace <new_file> with a file name of your choice, but you didn't.
What does " general/* " means?
In this context it means that all files in the folder general are to be added to the zip file.

Related

Python(on Win10): Path issue replacing "\" with "/"

Preface: I am a beginner to Python
Problem statement: I am writing a script wherein I will be launching an application (Gotit.exe) sitting at particular path lets say D:\Some Folder\SomeMore Folder\AgainFolder\myPythonFolder\Gotit.exe. I have kept the python-script also in myPythonFolder.
I am accessing the folder path via os.path.dirname(os.path.realpath(__file__)) and selecting particular application by appending it with \Gotit.exe but when passing the same appended string stored in a variable i.e. GotitexePath to os.system(GotitexePath) its throwing error as,
'D:\Some ' is not recognized as an internal or external command,
operable program or batch file.**
Kindly help me out to solve the said issue
I am using python 3.8.2 on Win10 Machine
The error is pointing to Some Folder name. Since there is a space in path you provide, the system doesn't know whether it is a part of folder name or it is a next argument to the command.
You need to escape the blank space. There are multiple ways to to it. For example wrap the path with double quotes:
"D:\Some Folder\SomeMore Folder\AgainFolder\myPythonFolder\Gotit.exe"
For more ways see this post
os.system("\"%s\"" % GotitexePath)
A the previous replies say, you need to add additional quotation marks around the path for the windows command line.

Syntax error when trying to open a file through Python 3 Command Shell

So I am learning Python through Udemy tutorials and now I need to open a file through CMD(CMD is opened on folder I need) and when I am typing function for opening file it says syntax error, but I have made everything good what a guy on tutorials says, I really don't know what what should I do, I checked all of the forums and still cant find the answer.
Here are some screenshots:
Couple of issues:
1.Your text file is called "example.txt.txt" instead of "example.txt"
2.The "example.txt","r" should be surrounded with brackets () instead of <>. These symbols look similar in cmd and are easy to confuse.
#instead of
file = open<"example.txt","r">
#use
file = open("example.txt","r")
This should fix your problem; let me know if it does.
You have to use parenthesss () not <>
file = open("example.txt","r")
check https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files

ValueError: need more than 0 values to unpack (Python 2)

I am trying to replicate another researcher's findings by using the Python file that he added as a supplement to his paper. It is the first time I am diving into Python, so the error might be extremely simple to fix, yet after two days I haven't still. For context, in the Readme file there's the following instruction:
"To run the script, make sure Python2 is installed. Put all files into one folder designated as “cf_dir”.
In the script I get an error at the following lines:
if __name__ == '__main__':
cf_dir, cf_file, cf_phys_file = sys.argv[1:4]
os.chdir(cf_dir)
cf = pd.read_csv(cf_file)
cf_phys = pd.read_csv(cf_phys_file)
ValueError: need more than 0 values to unpack
The "cf_file" and "cf_phys_file" are two major components of all files that are in the one folder named "cf_dir". The "cf_phys_file" relates only to two survey question's (Q22 and Q23), and the "cf_file" includes all other questions 1-21. Now it seems that the code is meant to retrieve those two files from the directory? Only for the "cf_phys_file" the columns 1:4 are needed. The current working directory is already set at the right location.
The path where I located "cf_dir" is as follows:
C:\Users\Marc-Marijn Ossel\Documents\RSM\Thesis\Data\Suitable for ML\Data en Artikelen\Per task Suitability for Machine Learning score readme\cf_dir
Alternative option in readme file,
In the readme file there's this option, but also here I cannot understand how to direct the path to the right location:
"Run the following command in an open terminal (substituting for file names
below): python cfProcessor_AEAPnP.py cf_dir cf_file cf_phys_file task_file jobTaskRatingFile
jobDataFile OESfile
This should generate the data and plots as necessary."
When I run that in "Command Prompt", I get the following error, and I am not sure how to set the working directory correctly.
- python: can't open file 'cfProcessor_AEAPnP.py': [Errno 2] No such file or directory
Thanks for the reading, and I hope there's someone who could help me!
Best regards & stay safe out there during Corona!!
Marc
cf_dir, cf_file, cf_phys_file = sys.argv[1:4]
means, the python file expects few arguments when called.
In order to run
python cfProcessor_AEAPnP.py cf_dir cf_file cf_phys_file task_file jobTaskRatingFile jobDataFile OESfile
the command prompt should be in that folder.
So, open command prompt and type
cd path_to_the_folder_where_ur_python_file_is_located
Now, you would have reached the path of the python file.
Also, make sure you give full path in double quotes for the arguments.

How to fix 'sh: 1: path/to/file not found ' python random generator

Hey all having a little issue with my code. I am creating a random food picker for my girlfriend whenever she tells me that she doesn't know where to eat
I am using aiys voice kit on a raspberry pi zero w and cloud text to speech API so the command process is like this
--- User Presses button
"Cloud speech is listening"
User Says "Where should we go for food"
run rndfoodpkr() ---
I also tried using omxplayer instead of mixer
def rndfoodpkr():
randomfile= random.choice(os.listdir(/home/pi/share/Programs/FoodPicker/Food/"))
file = '/home/pi/share/Programs/FoodPicker/Food' + randomfile + '/'
os.system ('mixer' + file)
rndfoodpkr() is supposed to run and select a random file inside the Food folder then mixer/omxplayer is supposed to run and play the .mp3 file however it gives this error
"sh: 1: mixer/home/pi/share/Programs/FoodPicker/Food/Wendys.mp3/: not found"
Same results for any file ie. Tacobell, McDonalds, arbys, etc
try:
file = '/home/pi/share/Programs/FoodPicker/Food' + randomfile
os.system ('mixer ' + file)
I see three problems with this code:
There is a quotation mark missing before /home on the second line. I suspect you introduced this error when posting your question, as your program would not be running at all if this error were present.
You are inserting a slash in the wrong place when you generate file -- the path you end up generating would look like
/home/pi/share/Programs/FoodPicker/FoodFilename.mp3/
^^ ^
instead of the intended
/home/pi/share/Programs/FoodPicker/Food/Filename.mp3
^ ^
A neat way of fixing this would be to assign the path to a variable instead of including it in your program twice, then use os.path.join() to combine the path components, e.g:
path = "/home/pi/share/Programs/FoodPicker/Food"
randomfile = os.path.join(path, random.choice(os.listdir(path)))
You are not including a space between the command mixer and the filename. Add one:
os.system("mixer " + randomfile)
^^^
Why are you suffixing the file name with a / character? That's possibly telling whatever application you're passing it to that it's a directory (so it could, for example, play every file in that directory - no necessarily the case but it certainly wouldn't be totally bizarre behavior).
See for example, the following transcript:
>> touch wendy.mp3
>> aplay wendy.mp3/ # Note the / at the end.
wendy.mp3/: Not a directory
In any case, I suspect the problem may be the missing / between the path and the file name, and the missing space between the command and the argument. That's likely to give you a full command of:
mixer/home/pi/share/Programs/FoodPicker/FoodWendys.mp4
So the first think I would try would be to move the / to where it belongs and put the space in, something like:
cmd = 'mixer /home/pi/share/Programs/FoodPicker/Food/{}'.format(randomfile)
cmd = 'mixer {}'.format(os.path.join('/home/pi/share/Programs/FoodPicker', randomfile))
The latter (with os.path.join) is considered more portable.
If your code still doesn't work after making those changes (or you want to debug), just print out the command before using it:
print("Command is '{}'".format(cmd))
A great many problems can be solved just by looking at what the computer is seeing(1).
(1) This also works for wives, apparently :-)
I found a solution myself that works great
def rndfoodpkr ():
path = "/home/pi/share/Programs/FoodPicker/Food"
randomfile = os.path.join(path, random.choice(os.listdir(path)))
mixer.init()
mixer.music.load(randomfile)
mixer.music.play()
Thank you guys for the help!

how to check for platform incompatible folder (file) names in python

I would like to be able to check from python if a given string could be a valid cross platform folder name - below is the concrete problem I ran into (folder name ending in .), but I'm sure there are some more special cases (e.g.: con, etc.).
Is there a library for this?
From python (3.2) I created a folder on Windows (7) with a name ending in dot ('.'), e.g. (without square brackets): [What I've done on my holidays, Part II.]
When the created folder was ftp'd (to linux, but I guess that's irrelevant), it did not have the dot in it anymore (and in return, this broke a lot of hyperlinks).
I've checked it from the command line, and it seems that the folder doesn't have the '.' in the filename
mkdir tmp.
dir
cd tmp
cd ..\tmp.
Apparently, adding a single dot at the end of the folder name is ignored, e.g.:
cd c:\Users.
works just as expected.
Nope there's sadly no way to do this. For windows you basically can use the following code to remove all illegal characters - but if someone still has a FAT filesystem you'd have to handle these too since those are stricter. Basically you'll have to read the documentation for all filesystem and come up with a complete list. Here's the NTFS one as a starting point:
ILLEGAL_NTFS_CHARS = "[<>:/\\|?*\"]|[\0-\31]"
def __removeIllegalChars(name):
# removes characters that are invalid for NTFS
return re.sub(ILLEGAL_NTFS_CHARS, "", name)
And then you need some "forbidden" name list as well to get rid of COM. Pretty much a complete mess that.. and that's ignoring linux (although there it's pretty relaxed afaik)
Do not end a file or directory name with a space or a period. Although
the underlying file system may support such names, the Windows shell
and user interface does not.
http://msdn.microsoft.com/en-us/library/aa365247.aspx#naming_conventions
That page will give you information about other illegal names too, for Windows that is. Including CON as you said your self.
If you respect those (seemingly harsh) rules, I think you'll be safe on Linux and most other systems too.

Categories