Python:在目录中创建以“.json”结尾的所有文件的zip文件(Python: Create a zip file of all files ending with “.json” in a directory)
假设目录是/ Home / Documents / Test_files。
我想创建一个以“.json”结尾的所有文件的zip文件,如果可能的话删除文件以便只保留zip文件
到目前为止,我已经能够创建给定路径中所有文件的zip文件,但是当我使用行
zipf.write(file)
它会抛出错误“[Errno 2]没有这样的文件或目录:sample.json” 。 但是,当我使用zipf.write(os.path.join(root, file))
它会写入文件,但也会写出我不想要的整个目录路径。我只是想自己编写文件。 当我使用
print file
,似乎print file
了正确的文件,所以我不知道为什么我得到文件不存在的错误目前我的代码如下所示:
def create_zip(path,zipf): #path is the directory address (i.e. /Home/Documents/Test_files) for root, dirs, files in os.walk(path): for file in files: if file.endswith(".json"): print file zipf.write(os.path.join(root, file)) #zipf.write(file)
我还想在创建zip文件后删除/删除文件以节省空间。
任何关于为什么会发生这种情况的帮助将不胜感激!
Let's say the directory is /Home/Documents/Test_files.
I would like to create a zip file of all the files ending with ".json" and if possible delete the files so that only the zip file is left
So far I have been able to create a zip file of all the files in the given path but when I use the line
zipf.write(file)
it throws the error "[Errno 2] No such file or directory: sample.json". However when I usezipf.write(os.path.join(root, file))
it does write the files but also the whole directory path which I don't want.I just want to write the files themselves. When I use
print file
the correct files seemed to be printed so I don't know why I get the error that the file doesn't existCurrently my code looks like this:
def create_zip(path,zipf): #path is the directory address (i.e. /Home/Documents/Test_files) for root, dirs, files in os.walk(path): for file in files: if file.endswith(".json"): print file zipf.write(os.path.join(root, file)) #zipf.write(file)
I would also like to remove/delete the files after creating the zip file to save space.
Any help as to why this is happening would be appreciated!
原文:https://stackoverflow.com/questions/36740683
最满意答案
好吧,我知道也许我应该把它放在评论中,但对不起,我没有足够的声誉去做。 无论如何我认为你的程序循环不停,因为你的start变量总是为NULL,首先调用函数会导致函数在发现start == NULL为真后调用自身,后续的调用总会导致无休止的递归,因为启动永远是NULL。 你写的这些线是黑洞 :
if (start == NULL) { start = LoadDeck(NULL); newest = start; }
它不会为启动变量分配任何内容,它只会导致无限递归。
Well, i know maybe i should put this in comment but sorry i don't have enough reputation to do it. Anyway i think your program loops endlessly because your start variable is always NULL, first call to the function is will lead to function to call itself after finding that start == NULL is true, the subsequent calls will always lead to an endless recursion because start is always NULL. these lines you wrote is the black hole:
if (start == NULL) { start = LoadDeck(NULL); newest = start; }
It doesn't assign anything to start variable, it just causes the endless recursion.
相关问答
更多-
TCP/IP模型是一个________。[2023-10-02]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
该输入应该可以用std::getline或std::regex_token_iterator进行解析(尽管后者是用大炮射击麻雀)。 例子: 正则表达式 快速和肮脏,但重量级的解决方案 (使用提升,所以大多数编译器吃这个) #include
#include int main() { const std::string s = "\"MARY\",\"PATRICIA\",\"LINDA\",\"BARBARA\",\"ELIZABETH\" ... -
您不需要带有区域和国家/地区的额外课程,它将与Map完美配合: private Map
> destinations = new HashMap<>(); 要使用文件中的值填充地图,您可以编写类似(未测试)的内容。 Scanner s = new Scanner(new File("Files/Destination.txt")); int currentCount = 0; String currentZone = ""; while(s.hasNextLine( ... -
您的json文件在以下.dependencies.pkgMeta.version无效: .dependencies.pkgMeta.version 。 修复json文件后,请尝试以下命令: jq ' .dependencies | to_entries | map(.value | { name: .main.name, c_version: .pkgMeta.version, n_version: .upd ...
-
好吧,我知道也许我应该把它放在评论中,但对不起,我没有足够的声誉去做。 无论如何我认为你的程序循环不停,因为你的start变量总是为NULL,首先调用函数会导致函数在发现start == NULL为真后调用自身,后续的调用总会导致无休止的递归,因为启动永远是NULL。 你写的这些线是黑洞 : if (start == NULL) { start = LoadDeck(NULL); newest = start; } 它不会为启动变量分配任何内容,它只会导致无限递归。 Well, i ...
-
#include
#include #include struct list { char *string; struct list *next; }; typedef struct list LIST; int main(void) { FILE *fp; char line[128]; LIST *current, *head; head = current = NULL; f ... -
使用C ++读取格式化文件(Formatted file reading with C++)[2023-05-02]
#include#include #include int main() { std::vector arr; std::ifstream f("file.txt"); int i; while (f >> i) arr.push_back(i); } 或者,使用标准算法: #include #include #include #include ... -
用cin读取格式化输入?(Read formatted input with cin?)[2023-06-25]
由于您需要DDMMYYYY格式,您可以拥有以下内容: std::string date ; std::getline( std::cin, date ); int dd,mm,yy; if ( date.size() == 8 ) // Other checkings left for you { mm = std::stoi( date.substr(0,2) ); dd = std::stoi( date.substr(2,2) ); yy = std::sto ... -
如果您检查例如此scanf (和系列)参考,您将看到"%s"格式代码 匹配一个字符串(一系列非空白字符) 因此,格式中的第一个"%s"匹配行中的所有字符,因为字段之间没有空格。 模式匹配不符合您的预期。 相反,你必须使用"%["格式: sscanf(string, "%[^%]%%%[^%]", key, country); 上面的格式字符串匹配除 '%'字符以外的所有字符,然后模式匹配'%'以丢弃它,然后再次匹配除尾随'%' 之外的所有字符。 另请注意,如果scanf无法匹配所有格式,则不会将指针设置为 ...